现在,当我添加具有hardCoded值的数据时,我想使用Http Request向服务器中添加一些数据,它将起作用,并给我一个响应代码200,但是当它来自用户时,它带有500响应代码和错误。 http请求助手类的代码。
public class JsonReader2 {
private String url;
private List<NameValuePair> pairs;
public JsonReader2(String url, List<NameValuePair> pairs) {
this.url = url;
this.pairs = pairs;
}
public JsonReader2(String url) {
this.url = url;
}
public String sendRequest() {
String response;
try {
// create HTTPURLConnection
URL url = new URL(this.url);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// set connection properties
connection.setReadTimeout(10000);
connection.setConnectTimeout(15000);
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(false);
// set value
OutputStream outputStream = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
writer.write(getQuery(pairs));
writer.flush();
writer.close();
outputStream.close();
// then connect
connection.connect();
Log.e("url",writer.toString());
Log.e("JsonReader",String.valueOf(connection.getResponseCode()));
// get response from connection
InputStream is ;
int status = connection.getResponseCode();
if (status != HttpURLConnection.HTTP_OK)
is = connection.getErrorStream();
else
is = connection.getInputStream();
// convert input stream to string response
Scanner s = new Scanner(is).useDelimiter("\\A");
response = s.hasNext() ? s.next() : "";
} catch (Exception e) {
e.printStackTrace();
response = null;
}
return response;
}
// method for converting the form of the data to request form
private String getQuery(List<NameValuePair> params) {
StringBuilder result = new StringBuilder();
boolean first = true;
try {
for (NameValuePair pair : params) {
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
}
} catch (Exception e) {
}
return result.toString();
}
}
Request类的代码
class GetDataRequest extends AsyncTask<String, Boolean, Boolean> {
@Override
protected Boolean doInBackground(String... strings) {
String name = strings[0];
String id_client = strings[1];
String map_address_client = strings[2];
String clientlat = strings[3];
String clientlag = strings[4];
String map_address_reciver = strings[5];
String reciver_lat = strings[6];
String reciver_lag = strings[7];
String delivery_time = strings[8];
String space = strings[9];
String name_receiver = strings[10];
String phone_receiver = strings[11];
String address_reciver = strings[12];
String street_number_reciver = strings[13];
String flower_number_reciver = strings[14];
String building_number_reciver = strings[15];
// if you have to send data to the databse
ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("lang", String.valueOf(MainActivity.lang)));
pairs.add(new BasicNameValuePair("name", name));
pairs.add(new BasicNameValuePair("id_client", id_client));
pairs.add(new BasicNameValuePair("map_address_client", map_address_client));
pairs.add(new BasicNameValuePair("client_lat", clientlat));
pairs.add(new BasicNameValuePair("client_lag", clientlag));
pairs.add(new BasicNameValuePair("map_address_reciver",map_address_reciver));
pairs.add(new BasicNameValuePair("reciver_lat", reciver_lat));
pairs.add(new BasicNameValuePair("reciver_lag",reciver_lag));
pairs.add(new BasicNameValuePair("delivery_time", delivery_time));
pairs.add(new BasicNameValuePair("space", space));
pairs.add(new BasicNameValuePair("name_reciver", name_receiver));
pairs.add(new BasicNameValuePair("phone_reciver", phone_receiver));
pairs.add(new BasicNameValuePair("address_reciver", address_reciver));
pairs.add(new BasicNameValuePair("street_number_reciver",street_number_reciver ));
pairs.add(new BasicNameValuePair("flower_number_reciver", flower_number_reciver));
pairs.add(new BasicNameValuePair("building_number_reciver", "jjj"));
com.mostafa.android.bsor3a.JsonReader2 j = new com.mostafa.android.bsor3a.JsonReader2(urluser, pairs);
result = j.sendRequest();
try {
JSONObject jsonObject = new JSONObject(result);
int messageId;
if (jsonObject.has("message")) {
JSONArray jsonArray = jsonObject.getJSONArray("message");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonobject = jsonArray.getJSONObject(i);
message = jsonobject.getString("message");
messageId = jsonobject.getInt("messageID");
}
}else if(jsonObject.has("data")){
JSONArray jsonArray = jsonObject.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonobject = jsonArray.getJSONObject(i);
message = jsonobject.getString("message");
messageId = jsonobject.getInt("messageID");
}
}
Toast.makeText(CompleteShippingActivity.this, ""+message, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
progDailog.cancel();
AlertDialog.Builder builder = new AlertDialog.Builder(CompleteShippingActivity.this);
builder.setMessage(message)
.setNegativeButton(getString(R.string.yes), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(CompleteShippingActivity.this, "onPostExecute", Toast.LENGTH_SHORT).show();
}
})
.create()
.show();
}
}
logcat的输出
那么我该如何解决该错误,并把 邮差工作得很好。
答案 0 :(得分:2)
500内部服务器错误表示服务器在响应您的请求时出现问题。您没有得到JSON字符串响应。
请再次检查您的请求。检查所有内容,包括标题,传递的参数。 将您的请求与工作中的邮递员请求进行比较。必须缺少一些参数。这会导致服务器响应您遇到麻烦。