2014-10-18 120 views
0

我試圖通過EditText獲取JSON值。JSON通過EditText獲取值

起初我有一堆空指針異常,解決了這個問題。但現在我的功能不起作用。包裝我的大腦在這...

我試圖創建一個EditText,獲取該值到一個字符串,並將其交給JSON對象。不知道我在做什麼錯...還是我忘了什麼?

public class MyActivity extends Activity { 
    TextView uid; 
    TextView name1; 
    TextView email1; 
    EditText edt; 
    Button Btngetdata; 

    //URL to get JSON Array 
    private static String url = "*"; 
    //JSON Node Names 
    private static final String TAG_TAG = "tag"; 
    private static final String TAG_ID = "id"; 
    private static final String TAG_LAST_NAME = "last_name"; 
    private static final String TAG_EMAIL = "country"; 

    JSONArray user = null; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_my); 
     Btngetdata = (Button)findViewById(R.id.getdata); 
     edt = (EditText)findViewById(R.id.edittext); 
     Btngetdata.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       new JSONParse().execute(); 
      } 
     }); 
    } 
    private class JSONParse extends AsyncTask<String, String, JSONObject> { 
     private ProgressDialog pDialog; 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      uid = (TextView)findViewById(R.id.uid); 
      name1 = (TextView)findViewById(R.id.name); 
      email1 = (TextView)findViewById(R.id.email); 

      pDialog = new ProgressDialog(MyActivity.this); 
      pDialog.setMessage("Getting Data ..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 
     } 
     @Override 
     protected JSONObject doInBackground(String... args) { 
      JSONParser jParser = new JSONParser(); 
      // Getting JSON from URL 
      JSONObject json = jParser.getJSONFromUrl(url); 
      return json; 
     } 
     @Override 
     protected void onPostExecute(JSONObject json) { 
      pDialog.dismiss(); 
      try { 
       // Getting JSON Array 
       user = json.getJSONArray(TAG_TAG); 
       JSONObject c = user.getJSONObject(0); 



       String xyz = edt.getText().toString(); 
       // Storing JSON item in a Variable 
       String id = c.getString(TAG_ID); 
       String name = c.getString(TAG_LAST_NAME); 
       String email = c.getString(TAG_EMAIL); 
       //Set JSON Data in TextView 
       uid.setText(id); 
       name1.setText(name); 
       email1.setText(email); 
       edt.setText(xyz); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

{ 「標籤」:[{ 「ID」: 「1」, 「FIRST_NAME」: 「菲利普」, 「姓氏」: 「搬運工」, 「電子郵件」:「[email protected] 」, 「國」: 「中國」, 「IP_ADDRESS」: 「26.83.255.206」},{ 「ID」: 「2」, 「FIRST_NAME」: 「南希」, 「姓氏」: 「馬丁」, 「電子郵件」: 「[email protected]」, 「國」: 「哥倫比亞」, 「IP_ADDRESS」: 「160.93.80.1」},{ 「ID」: 「3」, 「FIRST_NAME」: 「安」, 「姓氏」:「彼得森」, 「電子郵件」: 「[email protected]」, 「國」: 「中國」, 「IP_ADDRESS」: 「251.254.74.162」},{ 「ID」: 「4」, 「FIRST_NAME」: 「瑞秋」, 「姓氏」: 「克拉克」, 「電子郵件」: 「[email protected]」, 「國」: 「巴西」, 「IP_ADDRESS」: 「58.218.248.5」},{ 「ID」: 「5」,「FIRST_NAME 「:」 海瑟」, 「姓氏」: 「頓」, 「電子郵件」: 「[email protected]」, 「國」: 「埃塞俄比亞」, 「IP_ADDRESS」: 「244.69.119.16」},{ 「ID」: 「6」, 「如first_name」: 「露絲」, 「姓氏」: 「裏」,「EMA金正日 「:」 [email protected]」, 「國」: 「巴西」, 「IP_ADDRESS」: 「18.173.102.54」},{ 「ID」: 「7」, 「FIRST_NAME」: 「安德魯」, 「姓氏」 :「Turner」,「email」:「[email protected]」,「country」:「美國」,「ip_address」:「13.119.240.234」},{「id」:「8」,「first_name」: 「萬達」, 「姓氏」: 「麥地那」, 「電子郵件」: 「[email protected]」, 「國」: 「荷蘭」, 「IP_ADDRESS」: 「151.139.21.237」},{ 「ID」: 「9」,「first_name」:「Robert」,「last_name」:「Elliott」,「email」:「[email protected]」,「country」:「美國」,「ip_address」:「34.200.249.109」 },{ 「ID」: 「10」, 「FIRST_NAME」: 「凱文」, 「姓氏」: 「哈里森」, 「電子郵件」: 「[email protected]」, 「國」: 「巴西」, 「IP_ADDRESS」 : 「106.84.164.86」}]}

public class JSONParser { 
    static InputStream is = null; 
    static JSONObject jObj = null; 
    static String json = ""; 
    // constructor 
    public JSONParser() { 
    } 
    public JSONObject getJSONFromUrl(String url) { 
     // Making HTTP request 
     try { 
      // defaultHttpClient 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 
      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      is = httpEntity.getContent(); 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(
        is, "iso-8859-1"), 8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "n"); 
      } 
      is.close(); 
      json = sb.toString(); 
     } catch (Exception e) { 
      Log.e("Buffer Error", "Error converting result " + e.toString()); 
     } 
     // try parse the string to a JSON object 
     try { 
      jObj = new JSONObject(json); 
     } catch (JSONException e) { 
      Log.e("JSON Parser", "Error parsing data " + e.toString()); 
     } 
     // return JSON String 
     return jObj; 
    } 
} 

EDIT


JSONObject c = user.getJSONObject(Integer.parseInt(xyz)); 

我修改此,即時得到比默認1以外的響應時基值getJSONObject(0),但現在即時通訊進入1和即時得到2,在進入4得到5 ..

有誰知道如何解決這個問題?

+1

你可以發佈旅遊json嗎?也許你沒有得到正確的價值觀,作爲意見似乎確定 – zozelfelfo 2014-10-18 08:49:54

+0

我剛纔添加的JSON – 2014-10-18 09:05:31

+0

UID =(TextView的)findViewById(R.id.uid); name1 =(TextView)findViewById(R.id.name); email1 =(TextView)findViewById(R.id.email); 舉動這在OnCreate – 2014-10-18 09:09:54

回答

0

對於一個特定的位置嘗試這樣

int positionToShow = Integer.parseInt(edt.getText().toString()) - 1; 
user = json.getJSONArray(TAG_TAG); 
for(int i = 0; i < user.length(); i++) { 
    if(positionToShow == i) { 
     JSONObject c = user.getJSONObject(i); 
     // Storing JSON item in a Variable 
     String id = c.getString(TAG_ID); 
     String name = c.getString(TAG_LAST_NAME); 
     String email = c.getString(TAG_EMAIL); 
     uid.setText(id); 
     name1.setText(name); 
     email1.setText(email); 
    }  
} 
+0

這不返回任何 – 2014-10-18 09:44:29

+0

投入是System.out.print'(給GetString()更改爲optString() – kId 2014-10-18 09:47:26

+0

我應該把它放在哪裏?在list.add後面?在''loop *之外的list.add'之後的 – 2014-10-18 09:50:42

0

如果你試圖分析整個JSON數組然後使用this.In您發佈執行

List<String> allid = new ArrayList<String>(); 
JSONArray obj= jsonResponse.getJSONArray("tag"); 
for (int i=0; i<obj.length(); i++) { 
    JSONObject actor = cast.getJSONObject(i); 
    String id= actor.getString("id"); 
    allNames.add(id); 
} 

然後你可以使用一個for循環相應地獲得列表中的id。

否則,你可以做到這一點的解析使用GSON庫(你可以下載GSON庫here 在你的包只需創建一個類

public class DetailArray { 
    public boolean status; 
    public List<Detail_User> details; 

    public class Detail_User { 
     public String id; 
     public String first_name; 
     public String last_name; 
     public String email; 
     public String country; 
    } 
} 

在您的文章進行

如果響應是JSON響應

DetailArray detail1 = (new Gson()).fromJson(response.toString(), 
       DetailArray.class); 

現在假設,如果你想在列表視圖中設置名稱,然後

//return a arraylist 
private ArrayList<String> getName(DetailArray detail) { 
    ArrayList name = new ArrayList<String>(); 

     for (int i=0;i< detail.details.size();i++) { 
     name.add(splitStr[i]); 
     } 
    return names; 
} 

設置陣列適配器並將其設置爲一個列表

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
        this, 
        android.R.layout.simple_list_item_1, 
        getName(detail); 
     listview.setAdapter(arrayAdapter); 
0

我已經解析您的JSON ... ACC到輸入的號碼..和它顯示了確切的結果....請參考....

final EditText no=(EditText)findViewById(R.id.editText1); 
    final Button click=(Button)findViewById(R.id.button1); 
    click.setOnClickListener(new OnClickListener() 
    { 
     @Override 
     public void onClick(View arg0) 
     { 
      try 
      { 
       Toast.makeText(MainActivity.this, no.getText().toString(), 0).show(); 
       HttpClient client=new DefaultHttpClient(); 
       HttpGet request=new HttpGet("http://192.168.0.30/test.js"); 
       HttpResponse response=client.execute(request); 

       HttpEntity entity=response.getEntity(); 
       String json=EntityUtils.toString(entity); 

       JSONObject j1=new JSONObject(json); 
       JSONArray ja1=j1.getJSONArray("tag"); 
       JSONObject j2=ja1.getJSONObject(Integer.parseInt(no.getText().toString())-1); 

       Toast.makeText(MainActivity.this, j2.getString("first_name").toString() , 0).show(); 

       entity.consumeContent(); 
      } 
      catch(Exception e) 
      { 
       e.getStackTrace(); 
       Toast.makeText(MainActivity.this, e.toString() , 0).show(); 
      } 
     } 
    });