2014-04-13 3 views
0

저는 튜토리얼을 따르고 있으며 모든 것에 대해 올바르게 ac 코딩했다고 말할 수 있습니다. 그러나 나는이 묻는하지만 그들 중 누구도 내가 가지고있는 문제를 충족하는 것되지 않았에 의문을 제기 할 수 있습니다 알고org.json.jsonexception : org.json.JSONObject 타입의 value ...은 jsonarray로 변환 될 수 없습니다

org.json.jsonexception: value... of type org.json.JSONObject cannot be converted into jsonarray 

을 말한다 로그 고양이에 오류가 계속. 내 JSONParser 클래스

package com.nangu.r.a.t.s.JSONParser; 

import java.io.IOException; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.util.EntityUtils; 
import org.json.JSONArray; 
import org.json.JSONException; 
import android.util.Log; 

public class JSONParser { 

    public JSONArray GetDriverDetails(int driver_id) 
    { 
     String url = "http://10.0.2.2/www.ratsa.com/New_folder/refatted_db/get_driver_details.php?driver_id="+driver_id; 


     HttpEntity httpEntity = null; 

     try{ 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpGet httpGet = new HttpGet(url); 
      HttpResponse httpResponse = httpClient.execute(httpGet); 

      httpEntity = httpResponse.getEntity(); 
     }catch (ClientProtocolException e){ 
      e.printStackTrace(); 

     }catch (IOException e) { 
      e.printStackTrace(); 
     } 

     //Convert HttpEntity into JSON Array 
     JSONArray jsonArray = null; 
       if(httpEntity != null){ 
        try{ 
         String enttityResponse = EntityUtils.toString(httpEntity); 

         Log.e("Entity Response : ",enttityResponse); 

         jsonArray = new JSONArray(enttityResponse); 
        }catch(JSONException e){ 
         e.printStackTrace(); 

        }catch (IOException e) { 
         e.printStackTrace(); 
        } 
       } 

     return jsonArray; 
     } 


} 

을 내 mainactivity

package com.nangu.r.a.t.s; 

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.URLEncoder; 
import java.util.ArrayList; 
import java.util.List; 


import org.apache.http.NameValuePair; 
import org.apache.http.message.BasicNameValuePair; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.os.AsyncTask; 
import android.os.Bundle; 
import android.os.StrictMode; 
import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.content.Intent; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ProgressBar; 
import android.widget.TextView; 
import android.widget.Toast; 


import com.google.zxing.integration.android.IntentIntegrator; 
import com.google.zxing.integration.android.IntentResult; 
import com.nangu.r.a.t.s.JSONParser.JSONParser; 

public class MainActivity extends Activity implements OnClickListener { 

    private Button scanBtn; 
    private TextView formatTxt, resultTxt; 
    String scanContent; 
    EditText etDriverId; 
    int driver_id; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     scanBtn = (Button) findViewById(R.id.btnScan); 
     resultTxt = (TextView) findViewById(R.id.tvResult); 
     etDriverId = (EditText) findViewById(R.id.etDriverId); 

     scanBtn.setOnClickListener(this); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public void onClick(View v) { 
     if (v.getId() == R.id.btnScan) { 

      /* 
      * IntentIntegrator scanIntegrator = new IntentIntegrator(this); 
      * scanIntegrator.initiateScan(); 
      */ 
      scanContent = etDriverId.getText().toString(); 
      driver_id = Integer.parseInt(scanContent); 


       //this means the did is assigned. 
       new GetDriverDetails().execute(new JSONParser()); 


     } 
    } 

    public class GetDriverDetails extends AsyncTask<JSONParser, Long, JSONArray> 
    { 

     @Override 
     protected JSONArray doInBackground(JSONParser... params) { 
      //this is done in background thread 
      return params[0].GetDriverDetails(driver_id); 
     } 

     @Override 
     protected void onPostExecute(JSONArray jsonArray) { 
      try{ 
       JSONObject driver = jsonArray.getJSONObject(0); 


       resultTxt.setText(driver.getString("driver_first_name")); 

      }catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 
     } 


    } 
    public void onActivityResult(int requestCode, int resultCode, Intent intent) { 
     IntentResult scanningResult = IntentIntegrator.parseActivityResult(
       requestCode, resultCode, intent); 
     if (scanningResult != null) { 
      scanContent = scanningResult.getContents(); 
      String scanFormat = scanningResult.getFormatName(); 
      formatTxt.setText("FORMAT: " + scanFormat); 
      // contentTxt.setText("CONTENT: " + scanContent); 

     } else { 
      Toast toast = Toast.makeText(getApplicationContext(), 
        "No scan data received!", Toast.LENGTH_SHORT); 
      toast.show(); 
     } 

    } 


} 

사람이 올바른 방향으로 날 지점 수 있다면 난 정말 감사 드리겠습니다. 엔티티 응답

로그 캣 : 도움말 사람이 문제를 발견하기위한

04-13 23:40:17.768: E/Entity Response :(593): {"success":1,"driver":[{"driver_id":"1","driver_first_name":"Nangulukila ","driver_last_name":"Hacinzobolo","licence_class":"c","driver_dob":"1992-05-16","driver_adrress":"43 mulilansolo riverside","reg_date":"2011-04-05","reg_expiry":"2015-05-16"}]} 
04-13 23:40:17.788: W/System.err(593): org.json.JSONException: Value {"success":1,"driver":[{"licence_class":"c","reg_expiry":"2015-05-16","driver_last_name":"Hacinzobolo","driver_dob":"1992-05-16","driver_id":"1","reg_date":"2011-04-05","driver_adrress":"43 mulilansolo riverside","driver_first_name":"Nangulukila "}]} of type org.json.JSONObject cannot be converted to JSONArray 
+0

enttityResponse 변수의 값을 게시하십시오. 분명히 json 예외입니다. – drulabs

+0

아마도 jsonObject = new JSONObject (enttityResponse)를 사용해야합니다. 대신 jsonArray = 새로운 JSONArray (enttityResponse); –

+0

Amir 내가 return 문에서 불일치 오류가 다시 발생하면 변경합니다. –

답변

0

감사합니다 .... 내가 ... 다른 프로젝트에 대한 scriptwritten를 사용하던 내 PHP 스크립트 어리석은 실수에 있었다