无法将类型为org.json.jsonarray的值[{" code":" login_true"," name":" hhh"," email":" hhh"}]转换为jsonobject [英] Value[{"code":"login_true", "name":"hhh", "email":"hhh"}] of type org.json.jsonarray cannot be converted to jsonobject
问题描述
当编译器到达onPostExecute并尝试运行执行JSONArray时,行
jsonArray = jsonObject.getJSONArray( server_response);
抛出异常:
org.json.JSONException如:
值[{代码 : login_true , name < span class =code-string> : hhh , email : hhh }]
类型org.json.JSONArray无法转换为JSONObject。
我的尝试:
什么是正确的陈述?
我的代码:
protected String doInBackground(String ... params)
{
String call_type = params [0 ];
if(call_type.equals(login))
{
try {
URL url =新URL(login_url);
HttpURLConnection httpURLConnection =(HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod(POST);
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream OS = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS,UTF-8));
String email,pass;
email = params [1];
pass = params [2];
字符串数据= URLEncoder.encode(email,UTF-8)+=+ URLEncoder .encode(电子邮件,UTF-8)+& +
URLEncoder.encode(pass,UTF-8)+=+ URLEncoder.encode(pass,UTF-8);
< br $> b $ b bufferedWriter.write(data);
InputStream IS = httpURLConnection.getInputStream();
BufferedReader BR = new BufferedReader(new InputStreamReader(IS));
StringBuilder stringBuilder = new StringBuilder();
String line =;
while((line = BR.readLine())!= null)
{
stringBuilder.append(line +\ n);
}
bufferedWriter.flush();
bufferedWriter.close();
OS.close() ;
//IS.close();
httpURLConnection.disconnect();
Thread.sleep(500);
Log.d(Test,Test 3 pass);
返回stringBuilder.toString()。trim();
} catch(UnsupportedEncodingException e){
e.printStackTrace();
} catch(ProtocolException e){
e.printStackTrace();
} catch(MalformedURLException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
} catch(InterruptedException e){
e.printStackTrace();
}
}
返回null;
}
**强文** / *我的onPostExecute方法如下; * / ***
@Override
protected void onPostExecute(String json)
{
尝试
{
progressD ialog.dismiss();
JSONObject jsonObject = new JSONObject(json);
JSONArray jsonArray = jsonObject.getJSONArray(server_response) ;
******* //这里的异常来自org.json.JSONException:值[{code:login_true,name:hhh,email: org.json.JSONArray类型的hhh}]无法转换为JSONObject,编译器跳转到异常部分*******
JSONObject JO = jsonArray.getJSONObject(0);
字符串代码= JO.getString(code);
字符串消息= JO.getString(message);
if(code.equals(reg_true))
{
ShowDialog(注册成功,消息,代码);
}
else if(code.equals(reg_false))
{
ShowDialog(注册失败,消息,代码);
}
else if(code.equals(login_true))
{
Intent intent = new Intent(activity,HomeActivity.class);
intent.putExtra(message,message);
activity.startActivity(intent);
}
else if(code.equals( login_false))
{
ShowDialog(登录错误,消息,代码);
}
} catch(JSONException e){
e.printStackTrace();
}
}
** / * php脚本如下:(login.php)* / **
需要init.php; < br $>
$ email = $ _ POST [email];
$ pass = $ _ POST [pass];
$ sql_query =选择姓名,来自beneficiary_details的电子邮件,其中包含电子邮件,如'。$ email。'并传递像'。$ pass。';
$ result = mysqli_query($ con,$ sql_query);
$ response = array();
$ rowcount = mysqli_num_rows($ result);
// print_r($ rowcount );
if($ rowcount> 0)
{
$ row = mysqli_fetch_row($ result);
$ name = $ row [ 0];
$ email = $ row [1];
$ code =login_true;
array_push($ response,array(code=> $ code,name=> $ name,email=> $ email));
echo json_encode($ response) );
}
其他
{
$ code =login_false;
$ message =找不到用户,请再试一次..;
array_push($ response,array(code=> $ code,message=> $ message) );
echo json_encode($ response);
}
mysqli_close($ con);
?> ;
** plz找出我在做错的地方???
email =
< blockquote> _POST [email];
pass =
When compiler reaches onPostExecute and trying to run execute JSONArray the line
jsonArray=jsonObject.getJSONArray("server_response");
throws an exception:
org.json.JSONException like:
"Value[{"code":"login_true","name":"hhh","email":"hhh"}]
of type org.json.JSONArray cannot be converted to JSONObject".
What I have tried:
What is the correct statement?
My code:
protected String doInBackground(String... params)
{
String call_type=params[0];
if(call_type.equals("login"))
{
try {
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream OS = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
String email,pass;
email=params[1];
pass=params[2];
String data = URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(email, "UTF-8") + "&" +
URLEncoder.encode("pass", "UTF-8") + "=" + URLEncoder.encode(pass, "UTF-8");
bufferedWriter.write(data);
InputStream IS = httpURLConnection.getInputStream();
BufferedReader BR= new BufferedReader(new InputStreamReader(IS));
StringBuilder stringBuilder = new StringBuilder();
String line="";
while ((line=BR.readLine())!=null)
{
stringBuilder.append(line+"\n");
}
bufferedWriter.flush();
bufferedWriter.close();
OS.close();
//IS.close();
httpURLConnection.disconnect();
Thread.sleep(500);
Log.d("Test","Test 3 pass");
return stringBuilder.toString().trim();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
**strong text**/*my onPostExecute method is as below;*/***
@Override
protected void onPostExecute(String json)
{
try
{
progressDialog.dismiss();
JSONObject jsonObject = new JSONObject(json);
JSONArray jsonArray=jsonObject.getJSONArray("server_response");
*******//here exception coming org.json.JSONException: Value [{"code":"login_true","name":"hhh","email":"hhh"}] of type org.json.JSONArray cannot be converted to JSONObject and compiler jumps to the exception part*******
JSONObject JO= jsonArray.getJSONObject(0);
String code=JO.getString("code");
String message=JO.getString("message");
if(code.equals("reg_true"))
{
ShowDialog("Registration Success",message,code);
}
else if (code.equals("reg_false"))
{
ShowDialog("Registration Fail",message,code);
}
else if(code.equals("login_true"))
{
Intent intent= new Intent(activity,HomeActivity.class);
intent.putExtra("message",message);
activity.startActivity(intent);
}
else if(code.equals("login_false"))
{
ShowDialog("Login Error,",message,code);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
**/* php scrip is as follows: (login.php)*/**
require"init.php";
$email=$_POST["email"];
$pass=$_POST["pass"];
$sql_query="select name, email from beneficiary_details where email like '".$email."' and pass like '".$pass."' ";
$result=mysqli_query($con,$sql_query);
$response = array();
$rowcount=mysqli_num_rows($result);
//print_r( $rowcount);
if($rowcount > 0)
{
$row=mysqli_fetch_row($result);
$name=$row[0];
$email=$row[1];
$code="login_true";
array_push($response,array("code"=>$code,"name"=>$name,"email"=>$email));
echo json_encode($response);
}
else
{
$code="login_false";
$message="User not found, Please try again..";
array_push($response,array("code"=>$code,"message"=>$message));
echo json_encode($response);
}
mysqli_close($con);
?>
**plz find out where I am doing mistake???
email=
_POST["email"];
pass=
这篇关于无法将类型为org.json.jsonarray的值[{" code":" login_true"," name":" hhh"," email":" hhh"}]转换为jsonobject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!