2013-05-09 73 views
0

我是android開發新手。我有以下類以JSON格式下載一些數據。我一直在 HttpResponse httpResponse = httpClient.execute(httpPost); 線...我敢肯定,這必須是一個簡單的解決......這裏是類代碼...HttpClient.execute後找不到源錯誤()

package com.example.tankandroid; 

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.JSONException; 
import org.json.JSONObject; 

import android.util.Log; 

public 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 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; 

} 

} 
+0

你使用了什麼'url'? – 2013-05-09 21:08:55

+0

「http://www.simonsayssolutions.co.uk/index.php」 – HillInHarwich 2013-05-09 21:23:24

+0

This Source not found錯誤確實是誤導性的,你能確定這是什麼被捕獲爲例外嗎? – 2013-05-09 21:28:58

回答

-1

您需要提供一些我覺得更多的信息。你從哪裏得到「未找到源」錯誤?這是一個Eclipse錯誤,阻止你編譯。在編譯過程中嗎?它是運行時錯誤嗎?這可能是一個可能的重複:Source not found Android?

問題:如果您不打算添加任何POST數據,爲什麼要進行HTTP POST? GET似乎更合適。

既然你也問「我確定這肯定是一個簡單的修復」,那麼是的,它是。我真的建議你撕掉你的HTTP代碼並切換到Android Asynchronous Http Client。使用它非常容易,非常適合獲取HTTP響應並解析它。例如:

AsyncHttpClient client = new AsyncHttpClient(); 
RequestParams rp = new RequestParams(); 
rp.put("some_param", "some value"); 
rp.put("another_param", "some other value"); 
client.post("http://www.simonsayssolutions.co.uk/index.php", rp, new AsyncHttpResponseHandler() { 
    @Override 
    public final void onSuccess(String response) { 
     // handle your response and parse JSON here 
    } 

    @Override 
    public void onFailure(Throwable e, String response) { 
     // something went wrong 
    }    
}); 

或GET:

client.get("http://www.simonsayssolutions.co.uk/index.php", rp, new AsyncHttpResponseHandler() { 
... 
} 

最後,如果你想簡化JSON解析看看JacksonGson。特別是如果你想將JSON數據解析爲Java對象,反之亦然。

0

將這個代碼onCreate方法

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 
      .permitAll().build(); 
    StrictMode.setThreadPolicy(policy); 
0

使用Apache的HttpCore和HttpClient的庫。將這兩個庫放到lib文件夾中,它會自動將它們添加到您的構建路徑中。

0

這種情況的一個原因可能是AndroidManifest.xml文件中缺少Internet權限。在清單中添加此行可以解決問題。

<uses-permission android:name="android.permission.INTERNET" />