我不断收到此代码的内部服务器错误 500。我正在使用 Rest Web API。当我运行该程序时,它可以工作,但不会向 API 发送任何数据。当我运行调试器时得到的响应是:
响应{protocol=http/1.1,code=500,message=内部服务器错误,url=http://192.168.1.250:5001/api/v1/job}
我得到的JSON测试数据是:
{"id":"103","customer":{"id":31,"name":"ABC Polish"},"stops":{"Address":{"contact":{"name":"Play"}}},"references":{},"instructions":{"Value":"char"},"loads":{}}
这是完整的 AsyncTask 类代码,我有更多代码,但我认为添加它是没有意义的,而且你们更难帮助我:
public class myNetworkTask extends AsyncTask<Void, Void, Void> {
private ProgressBar progressBar;
protected Void doInBackground(Void... params) {
DBHandler db = new DBHandler(getApplicationContext());
int jobNoParam = jdb.getNextJobNo() - 1;
try {
URL url = new URL("http://192.168.1.250:5001/api/v1/job");
js.put("id", String.valueOf(jobNoParam));
JSONObject customerJSON = new JSONObject();
JSONObject stopsJson = new JSONObject();
JSONObject contactNameJson = new JSONObject();
JSONObject referencesJson = new JSONObject();
JSONObject instructionsJson = new JSONObject();
JSONObject addressJson = new JSONObject();
JSONObject loadsJson = new JSONObject();
if (jdb.getCustomerName(jobNoParam) != null && !jdb.getCustomerName(jobNoParam).equals("")) {
String idParam = jdb.getCustomerName(jobNoParam);
int id = db.getCompanyId(idParam);
customerJSON.put("id", id);
customerJSON.put("name", jdb.getCustomerName(jobNoParam) );
}
Log.i("sendDataToServer", "First if!");
if (jdb.getContactName(jobNoParam) != null && !jdb.getContactName(jobNoParam).equals("")) {
contactNameJson.put("name", jdb.getContactName(jobNoParam));
}
Log.i("sendDataToServer", "Second if!");
if (jdb.getJobType(jobNoParam) != null && jdb.getJobType(jobNoParam).equals("")) {
instructionsJson.put("Title", jdb.getJobType(jobNoParam));
}
Log.i("sendDataToServer", "Third if!");
if (jdb.getIssue(jobNoParam) != null && !jdb.getIssue(jobNoParam).equals("")) {
instructionsJson.put("Value", jdb.getIssue(jobNoParam));
}
// Log.i("sendDataToServer", "Fourth if!");
// js.put("Customer Name:", String.valueOf(customerJSON));
js.put("customer", customerJSON);
addressJson.put("contact", contactNameJson);
stopsJson.put("Address", addressJson);
js.put("stops", stopsJson);
js.put("references", referencesJson);
js.put("instructions", instructionsJson);
js.put("loads", loadsJson);
json = js.toString();
//Toast.makeText(getApplicationContext(), "Connected Successfully",
//Toast.LENGTH_LONG).show();
doPostRequest(url, json);
}
catch (MalformedURLException | JSONException e) {
e.printStackTrace();
}
return null;
}
protected Void onPostExecute () {
return null;
}
public void doPostRequest (URL url,final String json){
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new HttpLoggingInterceptor())
.build();
String credential = okhttp3.Credentials.basic(/*username*/, /*password */);
final RequestBody body = RequestBody.create(JSON, js.toString());
Request request = new Request.Builder()
.addHeader("Accept", "application/json")
.addHeader("Authorization", credential)
.url(url)
.post(body)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.d("TAG ------->", "Call Cancelled");
call.cancel();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.d("TAG --------> onResponse:", response.body().string());
Log.d("JSON TAG ---------->", json);
Log.d("TAG Response Message ------>", response.message());
}
});
}
}
最佳答案
您的问题中的代码 fragment 不是很清楚或没有帮助,因为您引用的变量我们无法在您发布的代码 fragment 中看到定义,但从我所看到的 JSON
应该按以下方式定义
公共(public)静态最终 MediaType JSON
= MediaType.parse("application/json; charset=utf-8");
您的请求正文应如下所示
final RequestBody body = RequestBody.create(JSON, json);
我建议尝试这个:
Response response = client.newCall(request).execute();
与排队直到收到服务器响应相反。
最后检查您是否使用正确的 header 将正确的数据发送到服务器
关于java - OKHttp3 POST 错误 : Internal Server Error Java Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50464634/