android

时间:2018-01-31 05:58:52

标签: android httpurlconnection

我在android中的HttpURLConnection中有500个响应代码。我从api调用中获取了一些数据。我无法找到问题。我搜索了很多并尝试过我自己但没有得到任何帮助。我在其他文件中有这样的代码并且它们运行良好。没有错误消息或异常。 这是我的代码段。

private void sendOfferAccepted(String  reqirement_id, final Context contex, String offer_id,String buyer_id, String status) {


        class HttpPost extends
                AsyncTask<Void, Void, String> {

            String mess = contex.getResources().getString(R.string.server_ip_api);

            String urlString = "https://"+mess+"/api/v1/accept_reject_supplier_offer/";

            private final String TAG = "post json example";

            private Context context;
            private String reqirement_id;
            private Object offer_id;
            private Object status;

            ProgressDialog progDailog = new ProgressDialog(contex);

            public HttpPost(String reqirement_id, final Context contex, String offer_id, String status) {


                this.context = contex;
                this.reqirement_id=reqirement_id;
                this.offer_id=offer_id;
                this.status=status;

            }

            @Override
            protected void onPreExecute() {
                Log.e(TAG, "1 - RequestVoteTask is about to start...");
                progDailog.setMessage("Loading...");
                progDailog.setIndeterminate(false);
                progDailog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                progDailog.setCancelable(true);
                progDailog.show();
            }

            @Override
            protected String doInBackground(Void... params) {
                String status = "false";

                String response = "";
                Log.e(TAG, "2 - pre Request to response...");

                try {
                    response = performPostCall(urlString, new HashMap<String, String>() {

                        private static final long serialVersionUID = 1L;
                        {
                            put("Accept", "application/json");
                            put("Content-Type", "application/json");
                        }
                    });
                    Log.e(TAG, "4 " + response.toString());
                    if(!response.equals(""))
                    {
                        //Toast.makeText(contex,"response is..."+response,Toast.LENGTH_LONG).show();
                        Log.e(TAG, "3 - give Response111111...");

                    }
                    else {
                        //Toast.makeText(contex,"some error ..."+response,Toast.LENGTH_LONG).show();
                    }


                } catch (Exception e) {
                    // displayLoding(false);

                    Log.e(TAG, "Error ..."+e);
                }
                Log.e(TAG, "5 - after Response...");

                if (!response.equals("")) {
                    try {
                        Log.e(TAG, "6 - response !empty...");
                        //

                        JsonParser jsonParser = new JsonParser();
                        JsonObject jsonobj = (JsonObject) jsonParser.parse(response.toString());

                        status=""+jsonobj.get("message");



                    } catch ( Exception e) {
                        // displayLoding(false);
                        // e.printStackTrace();
                        Log.e(TAG, "Error " + e.getMessage());
                    } finally {

                    }
                } else {
                    Log.e(TAG, "6 - response is empty...");

                    status = "false";
                }

                return status;
            }

            @Override
            protected void onPostExecute(String result) {
                //
                Log.e(TAG, "7 - onPostExecute ..."+result);
                Toast.makeText(context, ""+result, Toast.LENGTH_LONG).show();


                progDailog.dismiss();


            }

            public String performPostCall(String requestURL,
                                          HashMap<String, String> postDataParams) {

                URL url;
                String response = "";
                try {
                    url = new URL(requestURL);


                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

                    conn.setRequestMethod("POST");
                    conn.setDoInput(true);
                    conn.setDoOutput(true);

                    conn.setRequestProperty("Content-Type", "application/json");
                    Log.e(TAG,"final url is ..."+url);
                    Log.e(TAG, "11 - url : " + requestURL);

                    JSONObject root = new JSONObject();


                    root.put("requirement_id", reqirement_id);
                    root.put("offer_id", offer_id);
                    root.put("status", status);


                    Log.e(TAG, "12 - root : " + root.toString());

                    String str = root.toString();
                    byte[] outputBytes = str.getBytes("UTF-8");
                    OutputStream os = conn.getOutputStream();
                    os.write(outputBytes);

                    int responseCode = conn.getResponseCode();
                    Log.e(TAG, "13 - responseCode : " + responseCode);

                    if (responseCode == HttpsURLConnection.HTTP_OK) {
                        Log.e(TAG, "14 - HTTP_OK");

                        String line;
                        BufferedReader br = new BufferedReader(new InputStreamReader(
                                conn.getInputStream()));
                        while ((line = br.readLine()) != null) {
                            Log.e(TAG,"line is "+line);
                            response += line;
                        }
                        Log.e("error is ",response);
                    } else {
                        Log.e(TAG, "14 - False - HTTP_OK");
                        Log.e(TAG,response);
                        //  response = "";
                    }
                } catch (Exception e) {
                    Log.e(TAG,"exception is ..."+e);
                    e.printStackTrace();
                }

                Log.e("TAG","response is...."+response);
                return response;
            }
        }

        HttpPost sendPostReqAsyncTask = new HttpPost(reqirement_id,contex, offer_id, status);
        sendPostReqAsyncTask.execute();
    }

由于

0 个答案:

没有答案