0

我试图创建一个从 url 获取 json 对象的应用程序。这被证明是不必要的令人沮丧,因为它一直在应该加载和解析 json 对象的活动上崩溃。它只是弹出消息“不幸的是,(AppName)已停止。” 然后退出应用程序。JSON 中的数据永远不会显示在屏幕上。这是带有活动和 JSON 解析的代码

package com.example.Accomplist;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.RemoteException;
import android.widget.TextView;
import android.widget.Toast;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: DESAI_628IL
 * Date: 3/1/13
 * Time: 7:34 PM
 * To change this template use File | Settings | File Templates.
 */
public class MainScreen extends Activity{
    TextView myTextView;
   // HttpClient client;
    // url to make request
    final static String url = "http://accomplist.herokuapp.com/api/v1/sharedevent/2/?format=json";

    private static final String TAG_EVENT="event"; //A JSON object within the JSON object that will be returned by JSONParse()
    private static final String TAG_DESCRIPTION="description"; //A JSON tag within the JSON object EVENT
    private static String eventString="Yo";
    static JSONObject json;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_screen);
        new JSONParse().execute(url);
    }
private class JSONParse extends AsyncTask<String, String, String> {
    HttpClient client;
    JSONObject jsonObj= null;
    @Override
    protected String doInBackground(String... jsonurl) {
        StringBuilder url= new StringBuilder(String.valueOf(jsonurl));
        HttpGet get= new HttpGet(url.toString());
        HttpResponse r= null;
        try {
            r = client.execute(get);
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        int status= r.getStatusLine().getStatusCode();
        if (status==200){
            HttpEntity e=r.getEntity();
            String data= null;
            try {
                data = EntityUtils.toString(e);
            } catch (IOException e1) {
                e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
            try {
                jsonObj = new JSONObject(data);
            } catch (JSONException e1) {
                e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
            try {
                JSONObject eventJson= jsonObj.getJSONObject(TAG_EVENT);
                eventString= eventJson.getString(TAG_DESCRIPTION);
            }
            catch (JSONException e1) {
                eventString="Couldn't Parse Data";
            }
            return eventString;
        }
        else{
            return eventString;
        }
    }
    protected void onProgressUpdate(String... progress) {
        Toast loadingToast= Toast.makeText(getApplicationContext(), "Loading", Toast.LENGTH_LONG);
        loadingToast.show();
    }
    protected void onPostExecute(String result) {
        eventString=result;
        myTextView = (TextView)findViewById(R.id.textView1);
        myTextView.setText(eventString);
    }
}
}

我希望有人能帮帮忙。我被困在这个问题上的时间最长。我尝试了很多东西。我尝试了从 url 获取 JSON 对象的不同方法,将解析代码放在不同的类中,以及许多其他小事情。任何帮助,将不胜感激。这是 LogCat 错误报告

02-26 12:18:46.691: ERROR/ThrottleService(324): problem during onPollAlarm: java.lang.IllegalStateException: problem parsing stats: java.io.FileNotFoundException: /proc/net/xt_qtaguid/iface_stat_all: open failed: ENOENT (No such file or directory)
02-26 12:19:53.601: WARN/dalvikvm(4073): threadid=11: thread exiting with uncaught exception (group=0x40a71930)
02-26 12:19:55.121: WARN/dalvikvm(4073): threadid=12: thread exiting with uncaught exception (group=0x40a71930)
02-26 12:19:57.632: WARN/InputMethodManagerService(324): Got RemoteException sending setActive(false) notification to pid 4073 uid 10048
02-26 12:20:03.452: WARN/dalvikvm(4094): threadid=11: thread exiting with uncaught exception (group=0x40a71930)
02-26 12:20:04.744: ERROR/AndroidRuntime(4094): FATAL EXCEPTION: AsyncTask #1
        java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:299)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
        at java.util.concurrent.FutureTask.run(FutureTask.java:239)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
        at java.lang.Thread.run(Thread.java:856)
        Caused by: java.lang.IllegalArgumentException: Illegal character in path at index 0: [Ljava.lang.String;@40d12070
        at java.net.URI.create(URI.java:727)
        at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75)
        at com.example.Accomplist.MainScreen$JSONParse.doInBackground(MainScreen.java:84)
        at com.example.Accomplist.MainScreen$JSONParse.doInBackground(MainScreen.java:78)
        at android.os.AsyncTask$2.call(AsyncTask.java:287)
        at java.util.concurrent.FutureTask.run(FutureTask.java:234)
        ... 4 more
02-26 12:20:10.512: WARN/InputMethodManagerService(324): Got RemoteException sending setActive(false) notification to pid 4094 uid 10048
02-26 12:20:24.401: WARN/dalvikvm(4111): threadid=11: thread exiting with uncaught exception (group=0x40a71930)
02-26 12:20:24.441: ERROR/AndroidRuntime(4111): FATAL EXCEPTION: AsyncTask #1
        java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:299)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
        at java.util.concurrent.FutureTask.run(FutureTask.java:239)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
        at java.lang.Thread.run(Thread.java:856)
        Caused by: java.lang.IllegalArgumentException: Illegal character in path at index 0: [Ljava.lang.String;@40d12038
        at java.net.URI.create(URI.java:727)
        at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75)
        at com.example.Accomplist.MainScreen$JSONParse.doInBackground(MainScreen.java:84)
        at com.example.Accomplist.MainScreen$JSONParse.doInBackground(MainScreen.java:78)
        at android.os.AsyncTask$2.call(AsyncTask.java:287)
        at java.util.concurrent.FutureTask.run(FutureTask.java:234)
        ... 4 more
02-26 12:20:29.411: WARN/InputMethodManagerService(324): Got RemoteException sending setActive(false) notification to pid 4111 uid 10048
4

6 回答 6

0

试试这个

new JSONParse().execute("");

代替

new JSONParse().execute(url);

您可以粘贴您的 logcat 消息,以便我可以更准确地帮助您。

于 2013-03-07T06:06:32.423 回答
0

尝试这样的事情..

HttpClient client = new DefaultHttpClient();
        // Perform a GET request for a JSON list
        HttpUriRequest request = new HttpGet("https://somejson.json");
        // Get the response that sends back
        HttpResponse response = null;
        try {
            response = client.execute(request);
        } catch (ClientProtocolException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
于 2013-03-07T06:07:38.833 回答
0

中的参数doInbackgroud()像数组一样工作,因此您可以在调用方法时将多个参数传递给该execute方法AsyncTask。第一个传递的参数将存储在0th位置上,依此类推。由于您只将一个参数传递给执行方法,因此该参数将存储在0th位置...所以您需要从中获取 URLjsonurl[0]

尝试

StringBuilder url= new StringBuilder(String.valueOf(jsonurl[0]));
于 2013-03-07T06:11:53.903 回答
0

将您的 doInBackground 方法更改为:

  @Override
    protected String doInBackground(String... jsonurl) {
     StringBuilder url= new StringBuilder(String.valueOf(jsonurl[0]));
      HttpGet get= new HttpGet(url.toString());
      HttpResponse r= null;

目前您传递了错误的 url 来获取数据表单 web 服务。请参阅以下内容以了解我们如何从 doInBackground 方法中的varargs获取值

参数列表中的“...”是什么意思?doInBackground(字符串...参数)

于 2013-03-07T06:12:08.850 回答
0

你还没有得到你的 Http 客户端

HttpClient httpClient = new DefaultHttpClient(); 在你的 doInBackGround()

于 2013-03-07T06:16:14.483 回答
0

你有2个错误

1. StringBuilder url= new StringBuilder(String.valueOf(jsonurl[0])); 
  // jsonurl[0]

2. client = new DefaultHttpClient();                                
   // Add this line too
于 2013-03-07T06:17:06.957 回答