[英]json: unable to parse this type of json response
I know object is like some: {
and array like some: [
But when i do post request api response with, 我知道对象就像是一些:
{
和数组像是一些: [
但是当我发布请求api响应时,
{"code": 401, "message":"some message text"}
What kind of json is that, I had tried alot to display the message in a textview but none worked meanwhile I'm able to display object response or array... I'm using Volley
by the way, I had tried also to check 那是什么类型的json,我曾尝试过在textview中显示消息,但是在我能够显示对象响应或数组的同时却无济于事...我正在使用
Volley
,我也尝试过检查
If(response.has("message"){
}else{
}
And the same check for "code" to switch it to message from string resources Thanks in advance 和相同的“代码”检查将其从字符串资源切换到消息
HERE THE FULL REQUEST: 这里是完整的请求:
private void checkLogin(final String email, final String password) {
// Tag used to cancel the request
String tag_string_req = "req_login";
pDialog.setMessage("Logging in ...");
showDialog();
StringRequest strReq = new StringRequest(Method.POST,
AppConfig.URL_LOGIN, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Login Response: " + response.toString());
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
if (jObj.has("user")) {
// user successfully logged in
// Create login session
session.setLogin(true);
JSONObject user = jObj.getJSONObject("user");
String uid = user.getString("id_user");
String uname = user.getString("name");
String uemail = user.getString("email");
String utoken = user.getString("user_token");
// Inserting row in users table
db.addUser(uname, uemail, uid, utoken);
// Launch main activity
Intent intent = new Intent(LoginActivity.this,
Activity1.class);
startActivity(intent);
finish();
}else {
JSONObject jOsbj = new JSONObject(response);
TextView scss = (TextView) findViewById(R.id.loginerror);
scss.setText(jOsbj.getInt("code"));
String errorMsg = scss.getText().toString();
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Login Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {
@Override
protected Map<String, String> getParams() {
// Posting parameters to login url
Map<String, String> params = new HashMap<String, String>();
params.put("email", email);
params.put("password", password);
return params;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
below is the JSONResponse - 以下是JSONResponse-
{
"code": 401,
"message": "some message text"
}
u can parse it as below - 你可以解析如下-
assumming u have url response in Inputstream in then u read it in a stringBuffer buffer as below - 假设您在Inputstream中具有url响应,然后您在stringBuffer缓冲区中读取它,如下所示-
StringBuffer buffer = new StringBuffer(); int ch = -1; while ( (ch=in.read()) != -1){ buffer.append((char) ch); }
Now simply parse the response from buffer - 现在,只需解析缓冲区的响应-
JSONObject jObj = new JSONObject(buffer.toString()); String message = jObj.getString("message");
U r extracted message is in string message. 您提取的消息在字符串消息中。
you used 你用过
jsonObject jsonobject= new jsonObject("your json string");
String Code= jsonobject.getString(code);
String message= jsonobject.getString(message);
JSON STRING: JSON STRING:
{
"code": 401,
"message": "some message text"
}
Now create a seprate class for response: 现在创建一个单独的类进行响应:
public class ResponseDTO{
int code;
String message;
}
Now if you are using android studio add following to gradle or if you are using Eclipse you can find jar on link: http://www.java2s.com/Code/Jar/g/Downloadgson222jar.htm 现在,如果您使用的是android studio,请在gradle中添加以下内容;如果您使用的是Eclipse,则可以在链接上找到jar: http : //www.java2s.com/Code/Jar/g/Downloadgson222jar.htm
compile 'com.google.code.gson:gson:2.3'
After that use the following code to parse json string you already have: 之后,使用以下代码解析您已经拥有的json字符串:
String json = "the json you've recieved from server";
//ASSUMSE YOUR JSON IS NOT NULL
ResponseDTO response = new GsonBuilder().create().toJson(json.toString, ResponseDTO.class);
if (response != null) {
if (response.message.length() > 0 && response.message != null) {
//DO WHATEVER YOU WANT}
else{
//DO WHATEVER YOU WANT
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.