我试图通过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 ..
有谁知道如何解决这个问题?
你可以发布旅游json吗?也许你没有得到正确的价值观,作为意见似乎确定 – zozelfelfo 2014-10-18 08:49:54
我刚才添加的JSON – 2014-10-18 09:05:31
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