我试图通过EditText获取JSON值。
起初我有一堆nullpointer异常,解决了这个问题。但是现在我的功能还不起作用。把我的大脑包裹起来......
我尝试创建一个EditText,将该值获取为String,然后将其转换为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();
}
}
}
{&#34;标记&#34;:[{&#34; ID&#34;:&#34; 1&#34;&#34;如first_name&#34;:&#34;菲利普&#34; &#34;姓氏&#34;:&#34;波特&#34;&#34;电子邮件&#34;:&#34; pporter0@admin.ch",&#34;国家&#34 ;: &#34;中国&#34;&#34; IP_ADDRESS&#34;:&#34; 26.83.255.206&#34;},{&#34; ID&#34;:&#34; 2&#34 ;, &#34;如first_name&#34;:&#34;南西&#34;&#34;姓氏&#34;:&#34;马丁&#34;&#34;电子邮件&#34;:&#34; nmartin1 @ google.ca&#34;&#34;国家&#34;:&#34;哥伦比亚&#34;&#34; IP_ADDRESS&#34;:&#34; 160.93.80.1&#34;},{& #34; ID&#34;:&#34; 3&#34;&#34;如first_name&#34;:&#34;安&#34;&#34;姓氏&#34;:&#34;皮特森&# 34;&#34;电子邮件&#34;:&#34; apeterson2@utexas.edu",&#34;国&#34;:&#34;中国&#34;&#34; IP_ADDRESS&#34 ;:&#34; 251.254.74.162&#34;},{&#34; ID&#34;:&#34; 4&#34;&#34;如first_name&#34;:&#34;拉结&#34 ;,&#34;姓氏&#34;:&#34;克拉克&#34;&#34;电子邮件&#34;:&#34; rclark3@mayoclinic.com",&#34;国&#34; :&#34;巴西&#34;&#34; IP_ADDRESS&#34;:&#34; 58.218.248.5&#34;},{&#34; ID&#34;:&#34; 5&#34; &#34;如first_name&#34;:&#34;石楠&#34;&#34;拉st_name作为&#34;:&#34;波顿&#34;&#34;电子邮件&#34;:&#34; hburton4@creativecommons.org",&#34;国&#34;:&#34;埃塞俄比亚& #34;&#34; IP_ADDRESS&#34;:&#34; 244.69.119.16&#34;},{&#34; ID&#34;:&#34; 6&#34;&#34;如first_name& #34;:&#34;露丝&#34;&#34;姓氏&#34;:&#34;泳道&#34;&#34;电子邮件&#34;:&#34; rlane5@va.gov 34;,&#34;国家&#34;:&#34;巴西&#34;&#34; IP_ADDRESS&#34;:&#34; 18.173.102.54&#34;},{&#34; ID&# 34;:&#34; 7&#34;&#34;如first_name&#34;:&#34;安德鲁&#34;&#34;姓氏&#34;:&#34;特纳&#34;&# 34;电子邮件&#34;:&#34; aturner6@devhub.com",&#34;国家&#34;:&#34;美国&#34;,&#34; ip_address&#34;:&# 34; 13.119.240.234&#34;},{&#34; ID&#34;:&#34; 8&#34;&#34;如first_name&#34;:&#34;万达&#34;&# 34;姓氏&#34;:&#34;麦地那&#34;&#34;电子邮件&#34;:&#34; wmedina7@pagesperso-orange.fr",&#34;国&#34;:& #34;荷兰&#34;&#34; IP_ADDRESS&#34;:&#34; 151.139.21.237&#34;},{&#34; ID&#34;:&#34; 9&#34;,& #34; FIRST_NAME&#34;:&#34;罗伯特&#34;&#34;姓氏&#34;:&#34;埃利奥特&#34;&#34;电子邮件&#34;:&#34; relliott8 @乔omla.org&#34;,&#34; country&#34;:&#34; United States&#34;,&#34; ip_address&#34;:&#34; 34.200.249.109&#34;},{& #34; ID&#34;:&#34; 10&#34;&#34;如first_name&#34;:&#34;凯文&#34;&#34;姓氏&#34;:&#34;哈里森&# 34;,&#34;电子邮件&#34;:&#34; kharrison9@nih.gov",&#34;国家&#34;:&#34;巴西&#34;&#34; IP_ADDRESS&#34 ;:&#34; 106.84.164.86&#34;}]}
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;
}
}
修改
JSONObject c = user.getJSONObject(Integer.parseInt(xyz));
我对此进行了修改,当基值为getJSONObject(0)时,我得到的是除默认值1以外的响应,但是现在我输入1并且我得到2,输入4得到5 ..
有谁知道如何解决这个问题?
答案 0 :(得分: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);
}
}
答案 1 :(得分:0)
如果你试图解析整个json数组,那么使用它。在你发布执行
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);
答案 2 :(得分: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();
}
}
});