gpt4 book ai didi

java - 如何获取 Json 响应

转载 作者:行者123 更新时间:2023-11-30 08:49:27 25 4
gpt4 key购买 nike

我是 android 的新手.. 我想从 restful webservice 得到响应。网络服务工作正常。但是,如何从 android 获得响应。如果我运行该应用程序,请等待进度只来...请任何人帮助

这是我的代码

 public void invokeWS(RequestParams params){
// Show Progress Dialog
prgDialog.show();
// Make RESTful webservice call using AsyncHttpClient object
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://192.168.2.2:9999/useraccount/login/dologin", params, new JsonHttpResponseHandler() {
// When the response returned by REST has Http response code '200'
@Override
public void onSuccess(int statusCode, Header[] headers, String response) {
// Hide Progress Dialog
prgDialog.hide();
try {
// JSON Object
JSONObject obj = new JSONObject(response);
// When the JSON response has status boolean value assigned with true
if (obj.getBoolean("status")) {
Toast.makeText(getActivity().getApplicationContext(), "You are successfully logged in!", Toast.LENGTH_LONG).show();
// Navigate to Home screen
//navigatetoHomeActivity();
}
// Else display error message
else {
errorMsg.setText(obj.getString("error_msg"));
Toast.makeText(getActivity().getApplicationContext(), obj.getString("error_msg"), Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
Toast.makeText(getActivity().getApplicationContext(), "Error Occured [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show();
e.printStackTrace();

}
}

// When the response returned by REST has Http response code other than '200'
@Override
public void onFailure(int statusCode, Header[] headers, String content, Throwable error) {
// Hide Progress Dialog
prgDialog.hide();
// When Http response code is '404'
if (statusCode == 404) {
Toast.makeText(getActivity().getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
}
// When Http response code is '500'
else if (statusCode == 500) {
Toast.makeText(getActivity().getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
}
// When Http response code other than 404, 500
else {
Toast.makeText(getActivity().getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]", Toast.LENGTH_LONG).show();
}

}
});
}

上面代码中,Header[]在onSuccess方法中自动删除。如何修复它。请任何人帮助我!!

最佳答案

首先确保您拥有 list 上的权限:

<uses-permission android:name="android.permission.INTERNET" />

第二个是我建议你使用Volley请求。它易于使用,如果您使用的是 Android Studio,则插件库会更容易。只需将此行添加到您的 app.gradle 依赖项中即可。

compile 'com.mcxiaoke.volley:library:1.0.17'

有关镜像库的更多信息,请单击 here

现在进行示例实现:

public void SendRequest(View view){
progressDialog.setMessage("Please wait");
progressDialog.show();

String url ="YOUR_URL";

// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("Message", "Response is: " + response);
progressDialog.dismiss();

}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Message", "Error");
error.printStackTrace();
progressDialog.dismiss();
}
});

stringRequest.setTag("YOUR_TAG");
// Add the request to the RequestQueue.
queue.add(stringRequest);
}

并取消您的请求

public void CancelRequest(View view){
queue.cancelAll("YOUR_TAG");
Log.e("Message", "Request cancelled");
}

如果您需要更改要显示的 fragment ,为了更容易处理,只需在暂停或停止时添加它。祝你好运!

关于java - 如何获取 Json 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31399305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com