Android 异步任务上的JSON?
我正在制作一个Android程序,从互联网上获取JSON数据。JSON文本来自网页的源代码(使用HttpClient),然后解析它并将其显示到TextView。这是对Froyo的工作,但我需要它是对蜂窝和以上的工作任务 因为这个问题,我现在很困惑。我尝试并遵循了一些教程,但这与我想要做的不同。我只是犯了一些错误,我很沮丧。谢谢你的帮助!:) 这是我在MainActivity类上的方法:Android 异步任务上的JSON?,android,json,android-asynctask,httpclient,Android,Json,Android Asynctask,Httpclient,我正在制作一个Android程序,从互联网上获取JSON数据。JSON文本来自网页的源代码(使用HttpClient),然后解析它并将其显示到TextView。这是对Froyo的工作,但我需要它是对蜂窝和以上的工作任务 因为这个问题,我现在很困惑。我尝试并遵循了一些教程,但这与我想要做的不同。我只是犯了一些错误,我很沮丧。谢谢你的帮助!:) 这是我在MainActivity类上的方法: private void jsonStuffs() { // TODO Auto-ge
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();
}
}
}
}
}
将您的连接部分作为一个单独的类,并将下面的代码放入其中
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;
}
}
将您的连接部分作为一个单独的类,并将下面的代码放入其中
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;
}
}
将代码更改为使用AsyncTask进行webserivce调用:
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);
}
}
private void jsonStuffs(){
//TODO自动生成的方法存根
试一试{
新建InternetDataOperation()。执行(“”);
}
//捕获(JSONException e){
//e.printStackTrace();
//}
捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
私有类InternetDataOperation扩展了异步任务{
@凌驾
受保护的void onPreExecute(){
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
client=新的DefaultHttpClient();
GetMethodEx测试=新的GetMethodEx();
字符串jsonStr=test.getInternetData();//转到GetMethodEx
返回jsonStr;
}
@凌驾
受保护的void onPostExecute(字符串结果){
JSONObject obj=新JSONObject(结果);
//在网页上的JSON上查找温度
字符串温度=对象获取字符串(“温度”);
TextView tvTemp=(TextView)findViewById(R.id.temp);
tvTemp.setText(温度);
}
}
将您的代码更改为使用AsyncTask进行webserivce调用:
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);
}
}
private void jsonStuffs(){
//TODO自动生成的方法存根
试一试{
新建InternetDataOperation()。执行(“”);
}
//捕获(JSONException e){
//e.printStackTrace();
//}
捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
私有类InternetDataOperation扩展了异步任务{
@凌驾
受保护的void onPreExecute(){
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
client=新的DefaultHttpClient();
GetMethodEx测试=新的GetMethodEx();
字符串jsonStr=test.getInternetData();//转到GetMethodEx
返回jsonStr;
}
@凌驾
受保护的void onPostExecute(字符串结果){
JSONObject obj=新JSONObject(结果);
//在网页上的JSON上查找温度
字符串温度=对象获取字符串(“温度”);
TextView tvTemp=(TextView)findViewById(R.id.temp);
tvTemp.setText(温度);
}
}
您不能在doInBackground方法中使用findViewByID和textView。您可以在onPostExecute中使用textView
修改方法“jsonStuffs()”以便返回字符串。您不必将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();
}
}
}
}
}
然后创建如下异步任务:
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
}
}
}
公共类JsonDownloaderTask扩展了异步任务{
@凌驾
受保护的Void doInBackground(Void…参数){
返回jsonstaff()
}
@凌驾
受保护的void onPostExecute(字符串结果){
如果(结果!=null){
//使用TEXTVIEW完成您的工作
}
}
}
您需要在MainActivity.class中的onCreate()或onResume()方法中初始化textView。您不能在doInBackground方法中使用findViewByID和textView。您可以在onPostExecute中使用textView
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();
}
}
}
}
}
修改方法“jsonStuffs()”以便返回字符串。您不必将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();
}
}
}
}
}
然后创建如下异步任务:
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
}
}
}
公共类JsonDownloaderTask扩展了异步任务{
@凌驾
受保护的Void doInBackground(Void…参数){
返回jsonstaff()
}
@凌驾
受保护的void onPostExecute(字符串结果){
如果(结果!=null){
//使用TEXTVIEW完成您的工作
}
}
}
您需要在MainActivity.class中的onCreate()或onResume()方法中初始化textView。例如,我尝试将此代码放在AsyncTask类声明
textView TVTEM=(textView)findViewById(R.id.textView)下面代码>,我得到一个错误类型loadSomeStuff的findViewById(int)方法未定义。如果我把它放在onCreate方法上,它就不起作用了。它表示无法在onPostExecute()上解析TVTEM。我了解一点AsyncTask的逻辑,但我不太擅长Java编码。@KkeevvSienaAlejandrino:查看我的答案,将AsyncTask作为活动的内部类,而不是在单独的文件中创建它,如果要在单独的文件中创建它,则将活动上下文传递给它例如,我尝试将此代码放在AsyncTask类声明TextView tvTemp=(TextView)findViewById(R.id.TextView)下面代码>,我得到一个错误类型loadSomeStuff的findViewById(int)方法未定义。如果我把它放在onCreate方法上,它就不起作用了。上面说TVTEM不能在onPostExecute()上解析。我理解AsyncTask的一点逻辑,但我不太擅长Java编码。@KkeevvSienaAlejandrino:看到我的答案了吗
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();
}
}
}
}
}