[英]Android Studio networking.httpResponse@4e0cae2
我正在嘗試在Android Studio中創建應用程序,並且在嘗試解析JSON對象時遇到此錯誤。 我的JSON對象在這里https://api.myjson.com/bins/sk0rv
有人能幫我嗎?
第一次活動的代碼
public class HttpConnection extends AsyncTask<String, Void, HttpResponse> {
URL url;
HttpURLConnection connection;
@Override
protected HttpResponse doInBackground(String... params) {
try {
url=new URL(params[0]);
connection=(HttpURLConnection) url.openConnection();
InputStream inputStream=connection.getInputStream();
InputStreamReader inputStreamReader=new InputStreamReader(inputStream);
BufferedReader reader=new BufferedReader(inputStreamReader);
String line=reader.readLine();
reader.close();
inputStreamReader.close();
inputStream.close();
connection.disconnect();
return getHttpResponseFromJson(line);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
private HttpResponse getHttpResponseFromJson(String json) throws JSONException {
if(json==null){
return null;
}
JSONObject object=new JSONObject(json);
ExtraInfo extraInfo=getExtraInfoFromJson(object.getJSONObject("ExtraInfo"));
return new HttpResponse(extraInfo);
}
private ExtraInfo getExtraInfoFromJson(JSONObject object) throws JSONException {
if(object==null)
{
return null;
}
ExtraInfo extraInfo=new ExtraInfo();
extraInfo.setPretPromotie(object.getInt("pretPromotie"));
extraInfo.setNumeFilatelist(object.getString("numeFilatelist"));
JSONObject timbruInfo=object.getJSONObject("TimbruInfo");
if(timbruInfo!=null)
{
TimbruInfo timbru=new TimbruInfo();
timbru.setTara(timbruInfo.getString("taraOrigine"));
timbru.setTematica(timbruInfo.getString("tematica"));
timbru.setAnAparitie(timbruInfo.getInt("anAparitie"));
timbru.setPret(timbruInfo.getInt("pret"));
timbru.setTip(timbruInfo.getString("tip"));
}
return extraInfo;
}
}
第二項活動的代碼
public class PromotiiActivity extends AbstractActivity{
private static String URL="https://api.myjson.com/bins/sk0rv";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_promotii);
HttpConnection connection= new HttpConnection(){
@Override
protected void onPostExecute(HttpResponse httpResponse) {
super.onPostExecute(httpResponse);
if(httpResponse!=null)
{
Toast.makeText(getApplicationContext(), httpResponse.toString(), Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "is Empty", Toast.LENGTH_LONG).show();
}
}
};
connection.execute(URL);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuItem item = menu.findItem(R.id.app_menu_promotii);
item.setVisible(false);
return true;
}
}
這是因為您打印對對象的引用
Toast.makeText(getApplicationContext(), httpResponse.toString(), Toast.LENGTH_LONG).show();
你應該這樣做
HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity);
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.