响应在不同的Android应用程序中有所不同

时间:2016-09-09 12:19:56

标签: android android-asynctask response

当我在浏览器中输入URl时,我得到了正确的回复

 [{"responseCode":0},{"rewardPoints":-15000,"receipt":"20160909110957"}]   ,

但是当我试图通过android获得响应时,我得到了响应

[{"responseCode":111}]   ,  

我想得到像这样的回应

class Transid extends AsyncTask<String, String, String> {

    ProgressDialog  pdia;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pdia = new ProgressDialog(PopupActivity.this);
        pdia.setMessage("Loading...");
        pdia.show();
    }
    @Override
    protected String doInBackground(String... params) {
        HttpURLConnection connection = null;
        try {
            URL url = new URL(params[0]);
            connection = (HttpURLConnection) url.openConnection();

            connection.setRequestMethod("GET");
            connection.setReadTimeout(10000 /* milliseconds */);
            connection.setConnectTimeout(15000 /* milliseconds */);

            connection.setDoOutput(true);
            connection.connect();
            BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));

            char[] buffer = new char[1024];

            String jsonString = new String();

            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line + "\n");
            }
            br.close();

            jsonString = sb.toString();
            System.out.println(jsonString);
            JSONArray parentarray = new JSONArray(jsonString);
            JSONObject finalobject = parentarray.getJSONObject(1);
            transactionid = finalobject.getString("receipt");

            setTransactionid(transactionid);
            return ("" + transactionid);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


        return transactionid;
    }


    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
   }
 }

我在不同的应用程序中使用相同的代码并且工作正常

为什么我在此应用中得到不同的回复?

1 个答案:

答案 0 :(得分:1)

请参阅下面的工作代码。它经过测试且运行正常

 @Override
     protected String doInBackground(String... strings) {
         URL url;
         HttpURLConnection urlConnection = null;
         try {
             url=new URL("url?userId=4&points=15000&ipaddress=yourip");
             urlConnection = (HttpURLConnection) url.openConnection();
             urlConnection.setDoOutput(true);
             urlConnection.setDoInput(true);
             urlConnection.setRequestProperty("Content-Type", "application/json");
             urlConnection.setRequestMethod("GET");
             urlConnection.connect();

             int responseCode = urlConnection.getResponseCode();

             if(responseCode == HttpURLConnection.HTTP_OK){
                 String  server_response = readStream(urlConnection.getInputStream());
              Log.v("Yorclass.this.getClass().getName()",server_response);
             }

         } catch (MalformedURLException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         }
     }


 public  String readStream(InputStream in) {
        BufferedReader reader = null;
        StringBuffer response = new StringBuffer();
        try {
            reader = new BufferedReader(new InputStreamReader(in));
            String line = "";
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return response.toString();
    }

输出

  

[{&#34; responseCode&#34;:0},{&#34; rewardPoints&#34;: - 15000&#34;收据&#34;:&#34; 20160909110957&#34;}] < / p>