不同 android 应用的响应不同
Response is coming different in different android apps
当我在浏览器中输入 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);
}
}
我在不同的应用程序中使用相同的代码并且运行良好
为什么我在这个应用程序中得到不同的响应?
查看工作代码below.It 已经过测试并且工作正常
@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();
}
输出为
[{"responseCode":0},{"rewardPoints":-15000,"receipt":"20160909110957"}]
当我在浏览器中输入 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);
}
}
我在不同的应用程序中使用相同的代码并且运行良好
为什么我在这个应用程序中得到不同的响应?
查看工作代码below.It 已经过测试并且工作正常
@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();
}
输出为
[{"responseCode":0},{"rewardPoints":-15000,"receipt":"20160909110957"}]