2013-10-14 115 views
0

我想在我的應用程序中解析一些JSON。我有一個活動,用戶在他們的位置鍵入並按下一個按鈕,打開另一個活動。問題是應用程序在打開第二個活動時崩潰。我使用了一個教程來弄清楚如何獲取JSON數據,但無法弄清楚我做錯了什麼。我對Android開發非常陌生,因此非常感謝任何想法或幫助。在Android應用程序中獲取JSON

這裏是我的代碼:

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.view.View; 
import android.view.inputmethod.EditorInfo; 
import android.view.inputmethod.InputMethodManager; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.TextView.OnEditorActionListener; 

public class WeatherLocation extends Activity 
{ 
    EditText locationText; 
    TextView label; 
    Button getWeather; 
    String enteredText; 
    String url = "http://api.worldweatheronline.com/free/v1/weather.ashx?q=%s&format=json&num_of_days=5&key=37a5fj42xpyptvjgkhrx5rwu"; 
    String newURL; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.weatherlocation); 

     locationText = (EditText) findViewById(R.id.locationText); 
     label = (TextView) findViewById(R.id.label); 
     getWeather = (Button) findViewById(R.id.showWeather); 

     locationText.setText("Current Location"); 

     locationText.setOnEditorActionListener(new OnEditorActionListener() 
     { 
      @Override 
      public boolean onEditorAction(TextView v, int actionId, KeyEvent event) 
      { 
       boolean handled = false; 
       if (actionId == EditorInfo.IME_ACTION_DONE) 
       { 
        enteredText = locationText.getText().toString(); 
        System.out.println(enteredText); 

        // hide the virtual keyboard 
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
        imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 
               InputMethodManager.RESULT_UNCHANGED_SHOWN); 

        newURL = String.format(url, enteredText); 
        System.out.println("Formatted URL: " + newURL); 
        handled = true; 
       } 

       return handled; 
      } 
     }); 

     getWeather.setOnClickListener(new View.OnClickListener() 
     { 
      public void onClick(View v) 
      { 
       Intent weather = new Intent(WeatherLocation.this, Weather.class); 
       startActivity(weather); 
      } 
     }); 
    } 
} 

天氣信息類:

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.UnsupportedEncodingException; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.os.Bundle; 
import android.util.Log; 
import android.widget.TextView; 

public class Weather extends WeatherLocation 
{ 
    TextView currentTemp; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.weather); 

     currentTemp = (TextView) findViewById(R.id.currentTemp);  

     // Create instance of JSONParser 
     JSONParser jParser = new JSONParser(); 

     // getting JSON string from URL 
     System.out.println(newURL); 
     JSONObject json = jParser.getJSONFromUrl(newURL); 

     try 
     { 
      JSONObject data = new JSONObject(json.getString("data")); 
      JSONArray currentConditions = data.getJSONArray("current_condition"); 
      JSONArray weather = data.getJSONArray("weather"); 

      JSONObject temp = currentConditions.getJSONObject(0); 
      String fahr = temp.getString("temp_F"); 
      currentTemp.setText(fahr); 
     } 
     catch(Exception e) 
     { 
      e.getMessage().toString(); 
     } 
    } 

    public static class JSONParser 
    { 
     static InputStream is = null; 
     static JSONObject jObj = null; 
     static String json = ""; 

     // constructor 
     public JSONParser() 
     { 

     } 

     public JSONObject getJSONFromUrl(String url) 
     { 
      // Making HTTP request 
      try 
      { 
       // defaultHttpClient 
       DefaultHttpClient httpClient = new DefaultHttpClient(); 
       HttpPost httpPost = new HttpPost(url); 

       HttpResponse httpResponse = httpClient.execute(httpPost); 
       HttpEntity httpEntity = httpResponse.getEntity(); 
       is = httpEntity.getContent(); 
      } 
      catch (UnsupportedEncodingException e) 
      { 
       e.printStackTrace(); 
      } 
      catch (ClientProtocolException e) 
      { 
       e.printStackTrace(); 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
      } 

      try 
      { 
       BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); 
       StringBuilder sb = new StringBuilder(); 
       String line = null; 
       while ((line = reader.readLine()) != null) 
       { 
        sb.append(line + "\n"); 
       } 
       is.close(); 
       json = sb.toString(); 
      } 
      catch (Exception e) 
      { 
       Log.e("Buffer Error", "Error converting result " + e.toString()); 
      } 

      // try to parse the string to a JSON object 
      try 
      { 
       jObj = new JSONObject(json); 
      } 
      catch (JSONException e) 
      { 
       Log.e("JSON Parser", "Error parsing data " + e.toString()); 
      } 

      // return JSON String 
      return jObj; 
     } 
    } 
} 

錯誤:

10-14 13:11:28.523: E/AndroidRuntime(6583): FATAL EXCEPTION: main 
10-14 13:11:28.523: E/AndroidRuntime(6583): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kentuckyfarmbureau.kyfb/com.kentuckyfarmbureau.kyfb.Weather}: java.lang.NullPointerException 
10-14 13:11:28.523: E/AndroidRuntime(6583):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) 
10-14 13:11:28.523: E/AndroidRuntime(6583):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
10-14 13:11:28.523: E/AndroidRuntime(6583):  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
10-14 13:11:28.523: E/AndroidRuntime(6583):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
10-14 13:11:28.523: E/AndroidRuntime(6583):  at android.os.Handler.dispatchMessage(Handler.java:99) 
10-14 13:11:28.523: E/AndroidRuntime(6583):  at android.os.Looper.loop(Looper.java:137) 
10-14 13:11:28.523: E/AndroidRuntime(6583):  at android.app.ActivityThread.main(ActivityThread.java:5103) 
10-14 13:11:28.523: E/AndroidRuntime(6583):  at java.lang.reflect.Method.invokeNative(Native Method) 
10-14 13:11:28.523: E/AndroidRuntime(6583):  at java.lang.reflect.Method.invoke(Method.java:525) 
10-14 13:11:28.523: E/AndroidRuntime(6583):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
10-14 13:11:28.523: E/AndroidRuntime(6583):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
10-14 13:11:28.523: E/AndroidRuntime(6583):  at dalvik.system.NativeStart.main(Native Method) 
10-14 13:11:28.523: E/AndroidRuntime(6583): Caused by: java.lang.NullPointerException 
10-14 13:11:28.523: E/AndroidRuntime(6583):  at com.android.internal.os.LoggingPrintStream.println(LoggingPrintStream.java:298) 
10-14 13:11:28.523: E/AndroidRuntime(6583):  at com.kentuckyfarmbureau.kyfb.Weather.onCreate(Weather.java:42) 
10-14 13:11:28.523: E/AndroidRuntime(6583):  at android.app.Activity.performCreate(Activity.java:5133) 
10-14 13:11:28.523: E/AndroidRuntime(6583):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
10-14 13:11:28.523: E/AndroidRuntime(6583):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) 
10-14 13:11:28.523: E/AndroidRuntime(6583):  ... 11 more 

編輯: 我已編輯的代碼Weather.java(中第二項活動)包括:

Intent intent = getIntent(); 
     String url = intent.getStringExtra("INTENT_KEY_URL"); 

System.out.println(url); 
     JSONObject json = jParser.getJSONFromUrl(url); 
+1

在此處發佈堆棧跟蹤 – Raghunandan

+0

已添加錯誤。 – raginggoat

+0

wha tis line 42 Weather.java – Raghunandan

回答

2

您正在ui線程上運行網絡相關操作。

JSONObject json = jParser.getJSONFromUrl(newURL); 

在JsonParser你有

HttpResponse httpResponse = httpClient.execute(httpPost); 

使用threadAsyncTask

class TheTask extends AsyncTask<Void,Void,Void> 
    { 

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



    @Override 
    protected void onPreExecute() { 
     // TODO Auto-generated method stub 
     super.onPreExecute(); 

    } 

    @Override 
    protected Void doInBackground(Void... params) { 
      // get json from url here 
      getJSONFromUrl(String url); 
     return null; 
    } 

    } 

要調用new TheTask().execute()

http://developer.android.com/reference/android/os/AsyncTask.html

0

首先你必須發送你的「url」到第二個活動。要做到這一點,你可以使用意圖藉助額外的參數

像下面

Intent weather = new Intent(WeatherLocation.this, Weather.class); 
    weather .putExtra("INTENT_KEY_URL",newURL); 
    startActivity(weather); 

在你的第二個活動,你可以得到的URL到您的onCreate方法像下面

Intent intent =getIntent(); 
    String url =intent.getStringExtra("INTENT_KEY_URL"); 

那麼你可以使用它爲查詢你的json數據。

如果您可以發佈此錯誤的堆棧跟蹤,這非常有用。