gpt4 book ai didi

android - Json MySql 获取两个整数和一个字符串

转载 作者:行者123 更新时间:2023-11-29 14:28:10 26 4
gpt4 key购买 nike

这是我获取 JSON 对象的类。在这段代码中,我只得到一个对象,而且我真的不知道如何从该方法返回,有一个 Protected Void 方法,它是一个被调用的 settext 方法,并且是唯一的 JSON 对象所在的位置。

public class ConnectMySql extends Activity {

TextView httpStuff;
HttpClient client;
JSONObject json;

final static String URL = "http://79.114.48.119/RadarsMySql.php";

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
httpStuff = (TextView) findViewById(R.id.tvHttp);
client = new DefaultHttpClient();
new Read().execute("latitude");
}


public JSONObject lastTweet(String username) throws ClientProtocolException, IOException,JSONException{
StringBuilder url = new StringBuilder(URL);
url.append(username);


HttpGet get = new HttpGet(url.toString());
HttpResponse r = client.execute(get);
int status = r.getStatusLine().getStatusCode();
//if(status == 200){
HttpEntity e = r.getEntity();

String data = EntityUtils.toString(e);
data = data.substring(data.indexOf("["));

JSONArray timeline = new JSONArray(data);
JSONObject last = timeline.getJSONObject(0);
return last;

//}else{
//Toast.makeText(ConnectMySql.this, "error", Toast.LENGTH_LONG);
//return null;

//}
}

public class Read extends AsyncTask<String, Integer, String>{

@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try {
json = lastTweet("");
return json.getString(params[0]);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

@Override
public void onPostExecute(String result) {
// TODO Auto-generated method stub
httpStuff.setText(result);
int myNum = 0;

try {
myNum = Integer.parseInt(result);
httpStuff.setText(myNum);
} catch(NumberFormatException nfe) {
System.out.println("Could not parse " + nfe);
}
}

}

}

我想要做的是拥有一个可以存储三种对象的数组(例如纬度[1]、经度[1]、描述[1];纬度[2]等...我希望纬度和经度为整数)。之后,我将使用 for 循环来调用具有这 3 个参数的函数。有人可以帮助我或给我一些提示吗?谢谢!

最佳答案

不要使用 AsyncTask,因为它在后台执行,这就是它会给您带来一些问题的原因。

当你的 twitter arser 工作时,将其集成到 AsyncTask 中。下面的代码未经测试。

 public class ConnectMySql extends Activity {

TextView httpStuff;
HttpClient client;
int i;
JSONObject json;

final static String URL = "http://localhost/RadarsMySql.php";

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
httpStuff = (TextView) findViewById(R.id.tvHttp);
client = new DefaultHttpClient();
for(i=0;i<2;i++){
new Read().execute("latitude");

try {
json = lastTweet("",i);

String result = json.getString(params[i]);

httpStuff.setText(result);
int myNum = 0;

try {
myNum = Integer.parseInt(result);
httpStuff.setText(myNum);
} catch(NumberFormatException nfe) {
System.out.println("Could not parse " + nfe);
}


} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}


public JSONObject lastTweet(String username,int i) throws ClientProtocolException, IOException,JSONException{
StringBuilder url = new StringBuilder(URL);
url.append(username);


HttpGet get = new HttpGet(url.toString());
HttpResponse r = client.execute(get);
int status = r.getStatusLine().getStatusCode();
//if(status == 200){
HttpEntity e = r.getEntity();

String data = EntityUtils.toString(e);
data = data.substring(data.indexOf("["));

JSONArray timeline = new JSONArray(data);
JSONObject last = timeline.getJSONObject(i);
return last;

//}else{
//Toast.makeText(ConnectMySql.this, "error", Toast.LENGTH_LONG);
//return null;

//}
}

}

关于android - Json MySql 获取两个整数和一个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10537511/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com