2014-10-18 117 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(); 
      } 
     } 
    });