gpt4 book ai didi

Android:面临问题 -'BasicNetwork.performRequest: Unexpected response code 500'

转载 作者:太空狗 更新时间:2023-10-29 15:02:21 26 4
gpt4 key购买 nike

我正在尝试将数据从 Android 应用程序发送到 Servlet。我的 Android 应用程序运行正常。

问题仅与发送到 Servlet 的请求有关。它给出错误 BasicNetwork.performRequest: Unexpected response code 500

注意:我正在使用 Android Volley 库将我的请求发送到 Servlet。

下面我提到了: 1.我的Android应用程序代码 2.我的Servlet代码 3.日志

我的 Android 应用:MainActivity.java 文件

            package com.example.hawk;

import java.util.HashMap;
import java.util.Map;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements View.OnClickListener {

EditText var_sp = null;
EditText var_ect = null;
EditText var_er = null;
EditText var_iat = null;
EditText var_atp = null;
Button submit;
RequestQueue rq = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
v1 = (EditText) findViewById(R.id.sp);
v2 = (EditText) findViewById(R.id.ect);
v3 = (EditText) findViewById(R.id.er);
v4 = (EditText) findViewById(R.id.iat);
v5 = (EditText) findViewById(R.id.atp);
submit = (Button) findViewById(R.id.submit);
rq=Volley.newRequestQueue(this);
submit.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.submit:

new Thread(new Runnable() {
public void run() {
try{
StringRequest postReq = new StringRequest(Request.Method.POST, "http://192.168.0.103:8080/ServletABC",new Response.Listener<String>() {
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("var1",v1.getText().toString());
params.put("var2",v2.getText().toString());
params.put("var3",v3.getText().toString());
params.put("var4",v4.getText().toString());
params.put("var5",v5.getText().toString());
return params;
}

@Override
public void onResponse(String response) {
// TODO Auto-generated method stub

}

}, null);

rq.add(postReq);
}catch(Exception e)
{
e.printStackTrace();
}

}
}).start();

break;


}
}
}

我的小服务程序:

            import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class OBD_Feeder extends HttpServlet{
// JDBC driver name and database URL
public static final String JDBC_DRIVER="com.mysql.jdbc.Driver";
public static final String DB_URL="jdbc:mysql://localhost/Test";
// Database credentials
public static final String USER ="xxxx";
public static final String PASS ="xxxx";
Connection conn;
Statement stmt;

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
double v1=Double.parseDouble(request.getParameter("var1"));
double v2=Double.parseDouble(request.getParameter("var2"));
double v3=Double.parseDouble(request.getParameter("var3"));
double v4=Double.parseDouble(request.getParameter("var4"));
double v5=Double.parseDouble(request.getParameter("var5"));
Calendar calendar = Calendar.getInstance();
java.sql.Timestamp today_ts = new java.sql.Timestamp(calendar.getTime().getTime());

//System.out.println("TimeStamp: "+today_ts);

try{
// Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
// Open a connection
conn =DriverManager.getConnection(DB_URL,USER,PASS);

// Execute SQL query
stmt = conn.createStatement();
PreparedStatement pst =(PreparedStatement) conn.prepareStatement("insert into obd_test1(speed,coolant_temp,engine_rpm,in_air_temp,throttle_position, time_stamp) values(?,?,?,?,?,?)");

pst.setDouble(1,v1);
pst.setDouble(2,v2);
pst.setDouble(3,v3);
pst.setDouble(4,v4);
pst.setDouble(5,v5);
pst.setTimestamp(6,today_ts);
System.out.println("DB Query: "+pst.toString());
//Query execution
int i = pst.executeUpdate();
//conn.commit();
String msg=" ";
if(i!=0){msg="Record has been inserted";}
else{msg="failed to insert the data";}
System.out.println("DB Transaction Status: "+msg);
String docType ="<!doctype html public \"-//w3c//dtd html 4.0 "+"transitional//en\">\n";
out.println(docType +
"<html>\n"+
"<head><title>OBD Feeder</title></head>\n"+
"<body bgcolor=\"#f0f0f0\">\n"+
"<h1 align=\"center\">DATA Received:</h1>\n"+
"<ul>\n"+msg+"</ul>\n"+
"</body></html>");

pst.close();
stmt.close();
conn.close();
}catch(SQLException se){ se.printStackTrace();}//Handle errors for JDBC se.printStackTrace();
catch(Exception e){ e.printStackTrace();}//Handle errors for Class.forName e.printStackTrace();
finally
{ //finally block used to close resources
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){ se2.printStackTrace();}// nothing we can do
try{
if(conn!=null)
conn.close();
}catch(SQLException se){ se.printStackTrace();
}//end finally try
}
}
}

我的日志:

            08-15 10:42:55.128: I/HAWK(29403): POST Request: [ ] http://192.168.0.102:8080/OBD_Feeder 0x85adf656 NORMAL 3
08-15 10:42:55.408: E/Volley(29403): [1056] BasicNetwork.performRequest: Unexpected response code 500 for http://192.168.0.102:8080/ServletABC
08-15 10:42:55.408: D/HAWK(29403): Error: null
08-15 10:44:56.648: W/IInputConnectionWrapper(29403): getTextBeforeCursor on inactive InputConnection

最佳答案

检查你是否使用了正确的SDK

对于 Android Studio/IntelliJIDEA:

File -> Project Structure -> Project -> Project SDK 
Modules -> Check each modules "Module SDK"

您最好使用“Google API (x.x)”而不是 Android API

关于Android:面临问题 -'BasicNetwork.performRequest: Unexpected response code 500',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25322314/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com