Android 如何将字符串转换为JSONObject?
通过将数据字符串更改为jsonobject,我遇到了一个障碍,就是他的org.json.JSONException脚本error:Value 这是我正在尝试的编码Android 如何将字符串转换为JSONObject?,android,json,android-volley,Android,Json,Android Volley,通过将数据字符串更改为jsonobject,我遇到了一个障碍,就是他的org.json.JSONException脚本error:Value 这是我正在尝试的编码 btnLogin.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { String username = inputUser.getText().toString().trim();
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String username = inputUser.getText().toString().trim();
String password = inputPassword.getText().toString().trim();
// Check for empty data in the form
if (username.trim().length() > 0 && password.trim().length() > 0) {
Map<String,String> params = new HashMap<>();
params.put("username", inputUser.getText().toString());
params.put("password", inputPassword.getText().toString());
sendPostRequest(params);
} else {
// Prompt user to enter credentials
Toast.makeText(getApplicationContext(),
"Silahkan Masukan Username dan Password!", Toast.LENGTH_LONG)
.show();
}
}
});
public void sendPostRequest(Map<String, String> params) {
showDialog();
RequestQueue queue = Volley.newRequestQueue(this);
String url = "http://www.chris-chris.webege.com/login.php";
mCustomRequest = new CustomRequest(Request.Method.POST,
url, params, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Intent intent = new Intent(Login.this,
Coba.class);
startActivity(intent);
finish();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Error", error.getMessage());
hidepDialog();
Toast.makeText(Login.this, "Belum Terhubung Internet! ", Toast.LENGTH_SHORT).show();
}
});
mCustomRequest.setRetryPolicy(new DefaultRetryPolicy(Template.VolleyRetryPolicy.SOCKET_TIMEOUT,
Template.VolleyRetryPolicy.RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(mCustomRequest);
}
btnLogin.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
字符串username=inputUser.getText().toString().trim();
字符串密码=inputPassword.getText().toString().trim();
//检查表单中是否有空数据
if(username.trim().length()>0&&password.trim().length()>0){
Map params=新的HashMap();
params.put(“用户名”,inputUser.getText().toString());
params.put(“password”,inputPassword.getText().toString());
sendPostRequest(参数);
}否则{
//提示用户输入凭据
Toast.makeText(getApplicationContext(),
“Silahkan Masukan用户名和密码!”,Toast.LENGTH_LONG)
.show();
}
}
});
公共无效sendPostRequest(映射参数){
showDialog();
RequestQueue=Volley.newRequestQueue(this);
字符串url=”http://www.chris-chris.webege.com/login.php";
mCustomRequest=新的CustomRequest(Request.Method.POST,
url,参数,新响应。侦听器(){
@凌驾
公共void onResponse(JSONObject响应){
意图=新意图(Login.this,
Coba类);
星触觉(意向);
完成();
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Log.d(“Error”,Error.getMessage());
hidepDialog();
Toast.makeText(Login.this,“Belum Terhubung Internet!”,Toast.LENGTH_SHORT.show();
}
});
mCustomRequest.setRetryPolicy(新的DefaultRetryPolicy(Template.VolleyRetryPolicy.SOCKET)超时,
Template.VolleyRetryPolicy.RETRIES,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
添加(mCustomRequest);
}
试试这个
如果您调用服务并尝试将代码粘贴到isSucsess部件中
JSONObject jObject = jsonObject.getJSONObject("jsonobjectname");
tvTitle.setText(jObject.getString("your string name"));
将字符串转换为JSONObject有两个条件: 一,。字符串应采用正确的Json格式 二,。将代码放入try-catch块 例如:
String jsonString = "{"status":"1","message":"Login successfully","data":{"uid":"1","email":"baleshwar@gmail.com","password":"202cb962ac59075b964b07152d234b70","name":"","role":"1","gender":"","address":"","image":"","is_active":"0","last_login":"0000-00-00 00:00:00","device_id":"0","created_at":"2015-06-03 06:01:02","updated_at":"2015-06-03 11:01:02","location":"","dob":"0000-00-00","descriptions":""}}"
现在将此字符串转换为JSONObject
try{
JSONObject jsonResponse = new JSONObject(result);
}catch(Exception e){
e.printStackTrace();
}
可能的重复仍然存在错误,而JSONObject jObject=JSONObject的一部分无法读取他的JSONObject我尝试过代码,但仍然无法读取,因为它是输入中的参数,需要使用JSONObject与web匹配,