Android: Facing problem -BasicNetwork.performRequest: Unexpected response code 500 '
I am trying to send data from Android application to Servlet. Android app is working correctly.
The problem is only related to the request sent to the Servlet. It gives error BasicNetwork.performRequest: Unexpected response code 500
Note. I am using Android Volley library to send my request to a servlet.
Below I have mentioned: 1. My Android Application Code 2. My Servlet Code 3. LogCat
My Android app: MainActivity.java file
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;
}
}
}
My servlet:
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
}
}
}
My LogCat:
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
+3
source to share