2011-12-24 5 views
1

a java.io.FileNotFoundExceptionが発生します。ここに私のソースコードは次のとおりです。チップを追加するときにjava.io.FileNotFoundExceptionが発生する apiを使用すると、

public void addTip(final String venueId, final String text) { 
    new Thread() { 
     @Override 
     public void run() { 
      try { 
       URL url = new URL(FOURSQUARE_API 
         + "/tips/add" 
         + "?venueId=" + venueId 
         + "&text=" + text 
         + "&oauth_token=" + mAccessToken 
         + "&v=" + mVersion); 
       HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
       Log.d(TAG, "opening url " + url); 
       urlConnection.setRequestMethod("POST"); 
       urlConnection.setDoInput(true); 
       urlConnection.setDoOutput(true); 

       urlConnection.connect(); 

       try { 
       JSONObject response = getResponse(urlConnection); 
       String tipId = response.getString("id"); 
       String text = response.getString("text"); 
       Log.d(TAG, "tipId:" + tipId + "; " + "text:" + text); 
       } catch (Exception e) { 
        String response = streamToString(urlConnection.getErrorStream()); 
        Log.d(TAG, response); 
        throw e; 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }.start(); 
} 
    private JSONObject getResponse(HttpURLConnection urlConnection) throws Exception { 
    String response = streamToString(urlConnection.getInputStream()); 
    JSONObject jsonObject = (JSONObject) new JSONTokener(response).nextValue(); 
    JSONObject resp = jsonObject.getJSONObject("response"); 
    return resp; 
} 

private String streamToString(InputStream is) throws IOException { 
    String str = ""; 

    if (is != null) { 
     StringBuilder sb = new StringBuilder(); 
     String line; 

     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 

      while ((line = reader.readLine()) != null) { 
       sb.append(line); 
      } 

      reader.close(); 
     } finally { 
      is.close(); 
     } 

     str = sb.toString(); 
    } 

    return str; 
} 

とエラー情報:

 12-24 21:31:18.758: W/System.err(2356): java.io.FileNotFoundException: https://api.foursquare.com/v2/tips/add?venueId=4ef2cdd0b634355dee1f1794&text=classroom for sei students&oauth_token=CONS3IBOUEH2YTBA3IMNI3YH4CWSUKQ500QSOXJZQ23LXBH1&v=20111224 
    12-24 21:31:18.768: W/System.err(2356): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:521) 
    12-24 21:31:18.768: W/System.err(2356): at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:258) 
    12-24 21:31:18.768: W/System.err(2356): at com.ecnu.sei.manuzhang.study.FoursquareAPI.getResponse(FoursquareAPI.java:339) 
    12-24 21:31:18.768: W/System.err(2356): at com.ecnu.sei.manuzhang.study.FoursquareAPI.access$7(FoursquareAPI.java:338) 
    12-24 21:31:18.768: W/System.err(2356): at com.ecnu.sei.manuzhang.study.FoursquareAPI$2.run(FoursquareAPI.java:133) 

それは私が例外をキャッチした後getErrorStream()から何を取得奇妙です。 アイデアあなたは文書を見つけることができますhere

答えて

3

textビットをエンコードする必要があります。これを試してみてください:

+ "&text=" + URLEncoder.encode(text,"UTF-8"); 
+0

THXは、私は1ビットを助けない見落とすするための簡単なレッスン再び – manuzhang

+0

うん..と愚かなエラーメッセージを学んだ:) –

関連する問題