我正在制作一个从互联网上获取JSON数据的Android程序。 JSON文本来自网页的源代码(使用HttpClient),然后解析它并将其显示到TextView。这是在Froyo上工作,但我需要在AsyncTask上使用Honeycomb及以上版本。
由于这个AsyncTask问题,我现在很困惑。我尝试并遵循了一些教程,但它与我想要做的不同。我只是得到错误,我很沮丧。谢谢您的帮助! :)
这是我在MainActivity类上的方法:
private void jsonStuffs() {
// TODO Auto-generated method stub
//JSON PARSER & HOME PAGE TEXTVIEWS
client = new DefaultHttpClient();
//new Read().execute("id");
//http client codes only no parser !!
GetMethodEx test = new GetMethodEx();
String returned;
try {
returned = test.getInternetData();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try{
String jsonStr = test.getInternetData(); //go to GetMethodEx
JSONObject obj = new JSONObject(jsonStr);
//find temperature on JSON on webpage
String temperature = obj.getString("temperature");
TextView tvTemp = (TextView)findViewById(R.id.temp);
tvTemp.setText(temperature);
}
//catch (JSONException e) {
// e.printStackTrace();
//}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这是我的GetMethodEx类:
public class GetMethodEx extends Activity {
public String getInternetData() throws Exception{
BufferedReader in = null;
String data = null;
//
try{
HttpClient client = new DefaultHttpClient();
URI website = new URI("http://nhjkv.comuf.com/json_only.php");
HttpGet request = new HttpGet();
request.setURI(website);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.separator");
while ((l = in.readLine()) !=null){
sb.append(l + nl);
}
in.close();
data = sb.toString();
return data;
}finally {
if (in !=null){
try{
in.close();
return data;
} catch (Exception e){
e.printStackTrace();
}
}
}
}
}
答案 0 :(得分:1)
将您的连接部分作为一个单独的类并将下面的代码放在其中
public String getInternetData() throws Exception{
BufferedReader in = null;
String data = null;
//
try{
HttpClient client = new DefaultHttpClient();
URI website = new URI("http://nhjkv.comuf.com/json_only.php");
HttpGet request = new HttpGet();
request.setURI(website);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.separator");
while ((l = in.readLine()) !=null){
sb.append(l + nl);
}
in.close();
data = sb.toString();
return data;
}finally {
if (in !=null){
try{
in.close();
return data;
} catch (Exception e){
e.printStackTrace();
}
}
}
}
将此课程用作您的服务
public class TaskAsync extends AsyncTask<String, String, String> {
private String response = null;
@Override
protected String doInBackground(String... params) {
response = new ClassName().getInternetData();
return response;
}
}
然后将其放入您的活动中
String response = new TaskAsync().execute("URL_WITH_PARAMETERS").get();
答案 1 :(得分:1)
使用AsyncTask更改代码以进行Web服务调用:
private void jsonStuffs() {
// TODO Auto-generated method stub
try{
new InternetDataOperation().execute("");
}
//catch (JSONException e) {
// e.printStackTrace();
//}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private class InternetDataOperation extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... params) {
client = new DefaultHttpClient();
GetMethodEx test = new GetMethodEx();
String jsonStr = test.getInternetData(); //go to GetMethodEx
return jsonStr;
}
@Override
protected void onPostExecute(String result) {
JSONObject obj = new JSONObject(result);
//find temperature on JSON on webpage
String temperature = obj.getString("temperature");
TextView tvTemp = (TextView)findViewById(R.id.temp);
tvTemp.setText(temperature);
}
}
答案 2 :(得分:1)
你不能在doInBackground方法中使用findViewByID和textView。您可以在onPostExecute中使用textView。
修改方法“jsonStuffs()”以便返回String。而且您不必将getInternetData()放到另一个活动类中。
来自MainActivity.class的修改方法
private String jsonStuffs() {
// TODO Auto-generated method stub
//JSON PARSER & HOME PAGE TEXTVIEWS
client = new DefaultHttpClient();
//new Read().execute("id");
//http client codes only no parser !!
String returned;
try {
returned = getInternetData();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try{
String jsonStr = test.getInternetData(); //go to GetMethodEx
JSONObject obj = new JSONObject(jsonStr);
//find temperature on JSON on webpage
String temperature = obj.getString("temperature");
return temperature
}
//catch (JSONException e) {
// e.printStackTrace();
//}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
public String getInternetData() throws Exception{
BufferedReader in = null;
String data = null;
//
try{
HttpClient client = new DefaultHttpClient();
URI website = new URI("http://nhjkv.comuf.com/json_only.php");
HttpGet request = new HttpGet();
request.setURI(website);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.separator");
while ((l = in.readLine()) !=null){
sb.append(l + nl);
}
in.close();
data = sb.toString();
return data;
}finally {
if (in !=null){
try{
in.close();
return data;
} catch (Exception e){
e.printStackTrace();
}
}
}
}
}
然后像这样创建asynctask:
public class JsonDownloaderTask extends AsyncTask<Void, Void, String>{
@Override
protected Void doInBackground(Void... params) {
return jsonStuff()
}
@Override
protected void onPostExecute(String result){
if(result != null){
//DO YOUR STUFF WITH TEXTVIEW
}
}
}
您需要在MainActivity.class中的onCreate()或onResume()方法中初始化textView