2016-07-12 12 views
0

umgewandelt werden, wenn Compiler erreicht OnPostExecute und versuchen ausführen JSONArray die LinieJSONException: Wert kann nicht in JSONObject

jsonArray=jsonObject.getJSONArray("server_response"); 

löst eine Ausnahme auszuführen:

org.json.JSONException: 
    Value[{"code":"login_true","name":"hhh","email":"hhh"}] 
    of type org.json.JSONArray cannot be converted to JSONObject". 

Was ist die richtige Aussage?

Mein 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)*/** 
<?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??? 
+0

Bitte nehmen Sie [tour] (http : //stackoverflow.com/tour) bevor Sie die Nachricht senden – Shank

+0

Bitte zeigen Sie uns die 'JSON' zurückgegeben von der ser ver –

+0

Wird für diese Anweisung nicht die Ausnahme ausgelöst: JSONObject JO = jsonArray.getJSONObject (0); –

Antwort

1

Unter der Annahme, die JSON vom Server zurückgegeben wird:

[{"code":"login_true","name":"hhh","email":"hhh"}] 

die oben JSON analysieren:

try { 
    JSONArray jsonArray = new JSONArray(json); 
    JSONObject jsonObject = jsonArray.getJSONObject(0); 
    String code = jsonObject.getString("code"); 
    String name = jsonObject.getString("name"); 
} catch (JSONException e) { 
    e.printStackTrace(); 
} 
Verwandte Themen