我知道对象就像一些:{
和数组一样:[
但是当我发布请求api响应时,
{"code": 401, "message":"some message text"}
那是什么样的json,我曾经尝试过很多在textview中显示消息但是没有用,同时我能够显示对象响应或数组......我正在使用Volley
,我也试过检查
If(response.has("message"){
}else{
}
同样检查“代码”以将其切换为来自字符串资源的消息 提前致谢
这里有完整的要求:
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);
}
答案 0 :(得分:2)
是JSONResponse -
{
"code": 401,
"message": "some message text"
}
你可以解析它如下 -
假设你在Inputstream中有url响应然后你在stringBuffer缓冲区中读取它,如下所示 -
StringBuffer buffer = new StringBuffer();
int ch = -1;
while ( (ch=in.read()) != -1){
buffer.append((char) ch);
}
现在只需解析缓冲区的响应 -
JSONObject jObj = new JSONObject(buffer.toString());
String message = jObj.getString("message");
已解压缩的消息在字符串消息中。
答案 1 :(得分:1)
你用过
jsonObject jsonobject= new jsonObject("your json string");
String Code= jsonobject.getString(code);
String message= jsonobject.getString(message);
答案 2 :(得分:1)
JSON STRING:
{
"code": 401,
"message": "some message text"
}
现在为响应创建一个单独的类:
public class ResponseDTO{
int code;
String message;
}
现在,如果你使用android studio添加以下gradle,或者如果你正在使用Eclipse,你可以在链接上找到jar: http://www.java2s.com/Code/Jar/g/Downloadgson222jar.htm
compile 'com.google.code.gson:gson:2.3'
之后使用以下代码解析您已经拥有的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
}
}
}