2014-07-05 3 views
0

user_id을 JSON을 통해 PHP에 보내려고하지만 Error parsing data org.json.JSONException: Value 2 of type java.lang.Integer cannot be converted to JSONObject이 표시됩니다.Android : 데이터를 파싱하는 중 오류가 발생하여 JSONObject로 변환 할 수 없습니다.

어떻게하면됩니까? 미리 감사드립니다.

클래스 : 문제는 당신이 된 JSONObject로 정수 유형의 개체를 변환하려고

jsonobject = jsonarray.getJSONObject(i); 

이 라인에 의해 발생

public class Home1 extends Activity { 

JSONObject jsonobject; 
JSONArray jsonarray; 
ListView listview; 
ListViewAdapter1 adapterrr; 

SharedPreferences pref; 
String uid; 
String user_i,us; 
String ques_i; 
ArrayList<HashMap<String, String>> arraylist; 

//static String BET_ID = "bet_id"; 
static String QUESTION = "question"; 
static String QUES_ID = "ques_id"; 
static String ANSWER = "answer"; 

ArrayList<HashMap<String, String>> data; 



HashMap<String, String> resultp = new HashMap<String, String>(); 
Set<String> newset=new HashSet<String>(); 

@SuppressLint("NewApi") 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    pref = PreferenceManager.getDefaultSharedPreferences(this); 
    setContentView(R.layout.questionlist1); 

uid = pref.getString("user_id",null); 
Log.d("uid", ""+uid); 

Intent i = getIntent(); 
ques_i = i.getStringExtra("qsid"); 
Log.d("qsid", ""+ques_i); 


new DownloadJSON().execute(); 
} 


private class DownloadJSON extends AsyncTask<Void, Void, Void> { 

    /*protected ArrayList<NameValuePair> parameters; 


    public DownloadJSON() { 

    parameters = new ArrayList<NameValuePair>(); 

    NameValuePair us = new BasicNameValuePair("user_id", uid); 
    parameters.add(us); 
    }*/ 


    @Override 
    protected Void doInBackground(Void... params) { 


     arraylist = new ArrayList<HashMap<String, String>>(); 

     /*jsonobject = JSONfunctions 
       .getJSONfromURL("http://192.168.1.23/mutilatedphp/QuizGame/filtercheck.php");*/ 

     try { 

      jsonobject = JSONfunctions 
        .getJSONfromURL("http://192.168.1.23/mutilatedphp/QuizGame/filtercheck.php?user_id="+uid); 
      jsonarray = jsonobject.getJSONArray("ques"); 

      for (int i = 0; i < jsonarray.length(); i++) { 
       HashMap<String, String> map = new HashMap<String, String>(); 
       jsonobject = jsonarray.getJSONObject(i); 

       map.put("ques_id", jsonobject.getString("ques_id")); 
       map.put("question", jsonobject.getString("question")); 
       map.put("answer", jsonobject.getString("answer")); 


       arraylist.add(map); 
      } 
     } catch (JSONException e) { 
      Log.e("Error", e.getMessage()); 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void args) { 

     listview = (ListView) findViewById(R.id.listView2); 

     adapterrr = new ListViewAdapter1(Home1.this, arraylist); 

     listview.setAdapter(adapterrr); 


    } 
+0

서버의 json_response는 무엇입니까? – nitesh

답변

1

. 문제는 서버 측에 있습니다. 문제가 발견되면 답변을 게시하여 내가 여기 있는지 확인할 수 있습니다.

+0

이것은'user_id'가 전달되지 않을 때 보이는 링크입니다 : '{ "success": 0, "message": "No Categories found"}'. user_id를 추가하면 다음과 같이 표시됩니다 :'{ "ques": [{ "ques_id": "3", "question": "일본 화폐는 무엇입니까?", "answer": "Yen"}], "success" "message": "Successfully found"}' – MetaldroiD

0

나는 문제가 'ques'속성이 있는지 여부를 확인하지 않았다고 생각합니다. 존재하지 않는 속성에 getJSONArray() 메소드를 사용하면 JSON 라이브러리에서 예외가 발생하고 앱이 다운됩니다. 는 '성공'을을 확인하는 속성을 사용하여 완전히 괜찮습니다,

import org.json.JSONArray; 
import org.json.JSONObject; 

import java.util.HashMap; 
import java.util.Map; 

public class SimpleTest { 
    public static void main(String[] args) { 
     String notOk = "{\n" + 
       "\t\"success\":0,\n" + 
       "\t\"message\":\"No Categories found\"\n" + 
       "}"; 

     String ok = "{\n" + 
       "\t\"ques\":\n" + 
       "\t\t[\n" + 
       "\t\t\t{\n" + 
       "\t\t\t\t\"ques_id\":\"3\",\n" + 
       "\t\t\t\t\"question\":\"What is the currency of Japan?\",\n" + 
       "\t\t\t\t\"answer\":\"Yen\"\n" + 
       "\t\t\t}\n" + 
       "\t\t],\n" + 
       "\t\"success\":1,\n" + 
       "\t\"message\":\"Successfully found \"\n" + 
       "}"; 

     JSONObject okJSON = new JSONObject(ok); 
     JSONObject notOkJSON = new JSONObject(notOk); 

     System.out.println(new SimpleTest().parseJson(okJSON)); 
     System.out.println(new SimpleTest().parseJson(notOkJSON)); 
    } 

    public Map<String, String> parseJson(JSONObject jsonObject) { 
     if (jsonObject.has("ques")) { 
      JSONArray jsonArray = jsonObject.getJSONArray("ques"); 
      Map<String, String> map = new HashMap<String, String>(); 
      for (int i = 0; i < jsonArray.length(); i++) { 
       JSONObject tempObject = jsonArray.getJSONObject(i); 

       map.put("ques_id", tempObject.getString("ques_id")); 
       map.put("question", tempObject.getString("question")); 
       map.put("answer", tempObject.getString("answer")); 
      } 

      return map; 
     } else { 
      return new HashMap<String, String>(); 
     } 
    } 
} 

을 또는 :

당신은 내 작은 예를 살펴 수 있습니다, 그것은 더 아무것도하기 전에 'QUES'의 존재를 확인 'ques'속성이 있는지 여부를 알려줍니다. 희망을 갖고 대답 해 주시면 조금 도와 드리겠습니다.

관련 문제