【问题标题】:OKHttp3 POST Error : Internal Server Error Java AndroidOKHttp3 POST 错误:内部服务器错误 Java Android
【发布时间】:2018-05-22 09:48:26
【问题描述】:

我不断收到此代码的内部服务器错误 500。我正在使用 Rest Web API。当我运行程序时它可以工作,但它不会向 API 发送任何数据。我运行调试器时得到的响应是: Response{protocol=http/1.1, code=500, message=Internal Server Error, url=http://192.168.1.250:5001/api/v1/job}

我得到的 JSON 测试数据是: {"id":"103","customer":{"id":31,"name":"ABC Poland"},"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());
                        }
                    });
            }
        }

【问题讨论】:

    标签: java android android-asynctask okhttp3


    【解决方案1】:

    您在问题中剪断的代码不是很清楚或没有帮助,因为您引用的变量我们看不到您发布的代码 sn-p 中的定义,但从我可以看到JSON 应该定义如下

    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    您的请求正文应如下所示

    final RequestBody body = RequestBody.create(JSON, json);
    

    我建议试试这个:

    Response response = client.newCall(request).execute();
    

    而不是排队直到你得到服务器的响应。

    最后检查您是否使用正确的标头向服务器发送正确的数据

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-06
      • 1970-01-01
      • 2018-06-04
      • 1970-01-01
      • 2014-09-24
      • 2020-10-01
      • 2018-01-15
      • 1970-01-01
      相关资源
      最近更新 更多