2

我有一个主要活动,onclick 将 url 发送到另一个活动

如何添加条件,以便在单击特定行时我可以发送从 JSON 解析的特定 url


MainActivity.java

public class MainActivity extends Activity {

    // url to make request
    private static String url = "http://xxxxxxxxxxxx/";

    private HashMap<Integer, String> TimeMap = new HashMap<Integer, String>();

    List<Item> yourData = new ArrayList<Item>();

    ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Instantiating ProgressDialog with onCreate method
        progressDialog=new ProgressDialog(MainActivity.this);
        new ParsingAsync().execute();

    }

    private class ParsingAsync extends AsyncTask<Void, Void, Void>
    {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog=ProgressDialog.show(MainActivity.this, "", "Please Wait", true, false);


        }

        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub


            String _response = null;
            try
            {
            HttpClient httpclient = new DefaultHttpClient();
            httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
            HttpGet request = new HttpGet(url);
            HttpResponse response = httpclient.execute(request);
            HttpEntity resEntity = response.getEntity();
             _response =EntityUtils.toString(resEntity);
            JSONObject jsonObject = new JSONObject(_response);
            JSONArray first_array = jsonObject.getJSONArray("restaurants");
            JSONArray second_array = jsonObject.getJSONArray("RestaurantTimings");
             for(int i=0;i<first_array.length();i++)
             {
                    JSONObject c = second_array.getJSONObject(i);
                    Item item = new Item();

                    // Storing each json item in variable
                    int id = c.getInt("_id");
                    String TIME = c.getString("RestaurantTime");
                    item.setTime(TIME);

                    c = first_array.getJSONObject(i);

                    String NAME=c.getString("restaurantNAME");
                    item.setName(NAME);

                    String URL=c.getString("url");
                    item.setUrl(URL);

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

            return null;

        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            progressDialog.dismiss();

            //TextView timedisplay=(TextView) findViewById(R.id.RestaurantTimeID);


            ListView yourListView = (ListView) findViewById(R.id.listViewID);



            ListAdapter customAdapter = new ListAdapter(MainActivity.this, R.layout.itemlistrow, yourData);
            yourListView.setAdapter(customAdapter);
            yourListView.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {

                    Intent i = new Intent(MainActivity.this, RestaurantDesc.class);
                    i.putExtra("key", "http://54.218.73.244:7002/CopperChimney");
                    startActivity(i);

                }
            });
        }

    }

}

[编辑]

我尝试使用

i.putExtra("key", yourData.get(position).getUrl());

我可以在listview的第一个onclick上获得成功的数据,但在第二个,第三个......我得到以下错误

错误

08-25 19:14:57.133: D/dalvikvm(332): GC_EXTERNAL_ALLOC freed 165K, 50% free 2896K/5703K, external 2111K/2137K, paused 107ms
08-25 19:21:47.553: W/dalvikvm(370): threadid=6: spin on suspend #1 threadid=9 (pcf=0)
08-25 19:21:47.553: D/dalvikvm(370): Temporarily moving tid 378 to fg (was 0)
08-25 19:21:47.553: D/dalvikvm(370): Temporarily raised priority on tid 378 (10 -> 0)
08-25 19:21:47.633: W/dalvikvm(370): threadid=6: spin on suspend resolved in 1078 msec
08-25 19:21:47.633: D/dalvikvm(370): Restored policy of 378 to 0
08-25 19:21:47.633: D/dalvikvm(370): Restored priority on 378 to 10
08-25 20:10:30.833: W/dalvikvm(407): threadid=6: spin on suspend #1 threadid=9 (pcf=0)
08-25 20:10:30.833: D/dalvikvm(407): Temporarily moving tid 415 to fg (was 0)
08-25 20:10:30.833: D/dalvikvm(407): Temporarily raised priority on tid 415 (10 -> 0)
08-25 20:10:30.913: W/dalvikvm(407): threadid=6: spin on suspend resolved in 1079 msec
08-25 20:10:30.913: D/dalvikvm(407): Restored policy of 415 to 0
08-25 20:10:30.913: D/dalvikvm(407): Restored priority on 415 to 10
08-25 20:10:35.985: D/dalvikvm(407): GC_EXTERNAL_ALLOC freed 166K, 50% free 2896K/5703K, external 2111K/2137K, paused 85ms
08-25 20:10:46.523: W/System.err(407): org.json.JSONException: No value for CopperChimney
08-25 20:10:46.773: W/System.err(407):  at org.json.JSONObject.get(JSONObject.java:354)
08-25 20:10:46.773: W/System.err(407):  at org.json.JSONObject.getJSONArray(JSONObject.java:544)
08-25 20:10:46.773: W/System.err(407):  at com.project.findmybuffet.RestaurantDesc$ParsingAsync.doInBackground(RestaurantDesc.java:115)
08-25 20:10:46.783: W/System.err(407):  at com.project.findmybuffet.RestaurantDesc$ParsingAsync.doInBackground(RestaurantDesc.java:1)
08-25 20:10:46.783: W/System.err(407):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-25 20:10:46.783: W/System.err(407):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-25 20:10:46.783: W/System.err(407):  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-25 20:10:46.783: W/System.err(407):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-25 20:10:46.783: W/System.err(407):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-25 20:10:46.783: W/System.err(407):  at java.lang.Thread.run(Thread.java:1019)
08-25 20:10:46.793: D/AndroidRuntime(407): Shutting down VM
08-25 20:10:46.793: W/dalvikvm(407): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-25 20:10:46.813: E/AndroidRuntime(407): FATAL EXCEPTION: main
08-25 20:10:46.813: E/AndroidRuntime(407): java.lang.NullPointerException
08-25 20:10:46.813: E/AndroidRuntime(407):  at com.project.findmybuffet.RestaurantDesc$ParsingAsync.onPostExecute(RestaurantDesc.java:138)
08-25 20:10:46.813: E/AndroidRuntime(407):  at com.project.findmybuffet.RestaurantDesc$ParsingAsync.onPostExecute(RestaurantDesc.java:1)
08-25 20:10:46.813: E/AndroidRuntime(407):  at android.os.AsyncTask.finish(AsyncTask.java:417)
08-25 20:10:46.813: E/AndroidRuntime(407):  at android.os.AsyncTask.access$300(AsyncTask.java:127)
08-25 20:10:46.813: E/AndroidRuntime(407):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
08-25 20:10:46.813: E/AndroidRuntime(407):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-25 20:10:46.813: E/AndroidRuntime(407):  at android.os.Looper.loop(Looper.java:123)
08-25 20:10:46.813: E/AndroidRuntime(407):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-25 20:10:46.813: E/AndroidRuntime(407):  at java.lang.reflect.Method.invokeNative(Native Method)
08-25 20:10:46.813: E/AndroidRuntime(407):  at java.lang.reflect.Method.invoke(Method.java:507)
08-25 20:10:46.813: E/AndroidRuntime(407):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-25 20:10:46.813: E/AndroidRuntime(407):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-25 20:10:46.813: E/AndroidRuntime(407):  at dalvik.system.NativeStart.main(Native Method)
08-25 20:10:52.563: I/Process(407): Sending signal. PID: 407 SIG: 9
08-25 20:10:58.774: W/IInputConnectionWrapper(422): showStatusIcon on inactive InputConnection
08-25 20:25:43.273: D/dalvikvm(422): GC_EXTERNAL_ALLOC freed 160K, 49% free 2880K/5639K, external 2082K/2137K, paused 87ms
08-25 20:25:49.403: W/System.err(422): org.json.JSONException: No value for CopperChimney
08-25 20:25:49.473: W/System.err(422):  at org.json.JSONObject.get(JSONObject.java:354)
08-25 20:25:49.473: W/System.err(422):  at org.json.JSONObject.getJSONArray(JSONObject.java:544)
08-25 20:25:49.483: W/System.err(422):  at com.project.findmybuffet.RestaurantDesc$ParsingAsync.doInBackground(RestaurantDesc.java:115)
08-25 20:25:49.483: W/System.err(422):  at com.project.findmybuffet.RestaurantDesc$ParsingAsync.doInBackground(RestaurantDesc.java:1)
08-25 20:25:49.483: W/System.err(422):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-25 20:25:49.483: W/System.err(422):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-25 20:25:49.483: W/System.err(422):  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-25 20:25:49.493: W/System.err(422):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-25 20:25:49.493: W/System.err(422):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-25 20:25:49.493: W/System.err(422):  at java.lang.Thread.run(Thread.java:1019)
08-25 20:25:49.493: D/AndroidRuntime(422): Shutting down VM
08-25 20:25:49.503: W/dalvikvm(422): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-25 20:25:49.513: E/AndroidRuntime(422): FATAL EXCEPTION: main
08-25 20:25:49.513: E/AndroidRuntime(422): java.lang.NullPointerException
08-25 20:25:49.513: E/AndroidRuntime(422):  at com.project.findmybuffet.RestaurantDesc$ParsingAsync.onPostExecute(RestaurantDesc.java:138)
08-25 20:25:49.513: E/AndroidRuntime(422):  at com.project.findmybuffet.RestaurantDesc$ParsingAsync.onPostExecute(RestaurantDesc.java:1)
08-25 20:25:49.513: E/AndroidRuntime(422):  at android.os.AsyncTask.finish(AsyncTask.java:417)
08-25 20:25:49.513: E/AndroidRuntime(422):  at android.os.AsyncTask.access$300(AsyncTask.java:127)
08-25 20:25:49.513: E/AndroidRuntime(422):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
08-25 20:25:49.513: E/AndroidRuntime(422):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-25 20:25:49.513: E/AndroidRuntime(422):  at android.os.Looper.loop(Looper.java:123)
08-25 20:25:49.513: E/AndroidRuntime(422):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-25 20:25:49.513: E/AndroidRuntime(422):  at java.lang.reflect.Method.invokeNative(Native Method)
08-25 20:25:49.513: E/AndroidRuntime(422):  at java.lang.reflect.Method.invoke(Method.java:507)
08-25 20:25:49.513: E/AndroidRuntime(422):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-25 20:25:49.513: E/AndroidRuntime(422):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-25 20:25:49.513: E/AndroidRuntime(422):  at dalvik.system.NativeStart.main(Native Method)
08-25 20:26:51.563: W/dalvikvm(481): threadid=6: spin on suspend #1 threadid=9 (pcf=0)
08-25 20:26:51.563: D/dalvikvm(481): Temporarily moving tid 489 to fg (was 0)
08-25 20:26:51.563: D/dalvikvm(481): Temporarily raised priority on tid 489 (10 -> 0)
08-25 20:26:51.643: W/dalvikvm(481): threadid=6: spin on suspend resolved in 1080 msec
08-25 20:26:51.643: D/dalvikvm(481): Restored policy of 489 to 0
08-25 20:26:51.643: D/dalvikvm(481): Restored priority on 489 to 10
08-25 20:27:48.342: D/dalvikvm(481): GC_EXTERNAL_ALLOC freed 158K, 50% free 2896K/5703K, external 2111K/2137K, paused 134ms
08-25 20:28:07.754: D/dalvikvm(481): GC_FOR_MALLOC freed 353K, 44% free 3770K/6727K, external 2458K/3069K, paused 752ms
08-25 20:28:08.203: D/dalvikvm(481): GC_CONCURRENT freed 1K, 44% free 3770K/6727K, external 2458K/3069K, paused 7ms+16ms
08-25 20:28:08.843: W/System.err(481): org.json.JSONException: No value for CopperChimney
08-25 20:28:08.843: W/System.err(481):  at org.json.JSONObject.get(JSONObject.java:354)
08-25 20:28:08.853: W/System.err(481):  at org.json.JSONObject.getJSONArray(JSONObject.java:544)
08-25 20:28:08.883: W/System.err(481):  at com.project.findmybuffet.RestaurantDesc$ParsingAsync.doInBackground(RestaurantDesc.java:115)
08-25 20:28:08.883: W/System.err(481):  at com.project.findmybuffet.RestaurantDesc$ParsingAsync.doInBackground(RestaurantDesc.java:1)
08-25 20:28:08.913: W/System.err(481):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-25 20:28:08.913: W/System.err(481):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-25 20:28:08.913: W/System.err(481):  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-25 20:28:08.923: W/System.err(481):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-25 20:28:08.933: W/System.err(481):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-25 20:28:08.943: W/System.err(481):  at java.lang.Thread.run(Thread.java:1019)
08-25 20:28:08.943: D/AndroidRuntime(481): Shutting down VM
08-25 20:28:08.943: W/dalvikvm(481): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-25 20:28:08.963: E/AndroidRuntime(481): FATAL EXCEPTION: main
08-25 20:28:08.963: E/AndroidRuntime(481): java.lang.NullPointerException
08-25 20:28:08.963: E/AndroidRuntime(481):  at com.project.findmybuffet.RestaurantDesc$ParsingAsync.onPostExecute(RestaurantDesc.java:138)
08-25 20:28:08.963: E/AndroidRuntime(481):  at com.project.findmybuffet.RestaurantDesc$ParsingAsync.onPostExecute(RestaurantDesc.java:1)
08-25 20:28:08.963: E/AndroidRuntime(481):  at android.os.AsyncTask.finish(AsyncTask.java:417)
08-25 20:28:08.963: E/AndroidRuntime(481):  at android.os.AsyncTask.access$300(AsyncTask.java:127)
08-25 20:28:08.963: E/AndroidRuntime(481):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
08-25 20:28:08.963: E/AndroidRuntime(481):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-25 20:28:08.963: E/AndroidRuntime(481):  at android.os.Looper.loop(Looper.java:123)
08-25 20:28:08.963: E/AndroidRuntime(481):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-25 20:28:08.963: E/AndroidRuntime(481):  at java.lang.reflect.Method.invokeNative(Native Method)
08-25 20:28:08.963: E/AndroidRuntime(481):  at java.lang.reflect.Method.invoke(Method.java:507)
08-25 20:28:08.963: E/AndroidRuntime(481):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-25 20:28:08.963: E/AndroidRuntime(481):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-25 20:28:08.963: E/AndroidRuntime(481):  at dalvik.system.NativeStart.main(Native Method)

我希望我的解释清楚。有任何想法吗?谢谢。

4

3 回答 3

1

你可以试试这个。改变你的线路

i.putExtra("key", "http://54.218.73.244:7002/CopperChimney");

i.putExtra("key", yourData.get(position).getUrl());

编辑

而不是 hardcoding "CopperChimney",尝试传递另一个额外的:

i.putExtra("name", yourData.get(position).getName());

并在RestauranDesc课堂上使用它(请记住将其初始化为与您的url变量相同:

first_array = jsonObject.getJSONArray(name);
于 2013-08-25T14:39:22.123 回答
0

所以你的问题是如何在活动之间转换数据?只需使用 Bundle 或 Intent

于 2013-08-25T14:37:52.313 回答
0

从您的评论中,您有 NullPointerException

for(int i=0; i < first_array.length(); i++) 

first_array也一样

为什么它是空的?

first_array = jsonObject.getJSONArray("CopperChimney");  

您的 json 没有名为 CopperChimney 的 json 数组

restaurants并且RestaurantTimings是数组。

{//代表json对象节点

[//代表json数组节点

你有

{  // json object node
  "restaurants": [ // json array by name restaurants

"RestaurantTimings": [ // json array by name RestaurantTimings

所以我在你的 json 中的任何地方都看不到CopperChimneyjson 数组

解析

您的餐厅名称之一是 CopperChimney

  try
    {
    JSONObject jsonObject = new JSONObject(_response);

     JSONArray jarr = jsonObject.getJSONArray("restaurants");
             // json array restaurants
      for(int i=0;i<jarr.length();i++)
      {
          JSONObject jb = jarr.getJSONObject(i); 
          String id = jb.getString("restaurantID");
          String name = jb.getString("restaurantNAME"); // restaurants name
          Toast.makeText(getApplicationContext(),name, 1000).show(); 
      }
    }
    catch(Exception e)
    {
           e.printStacktrace();
    }
于 2013-08-25T15:23:13.667 回答