1. In regards to recently combined carrier specific root forums, if your device forum was affected, please see this poll/thread!

java.net.UnknownHostException


  1. jms_gears1

    jms_gears1 Member

    Im attempting to build an application that sends a request to a server.
    The server spits out JSON encoded text.
    I want the app to get that text and display that information/store it in the droids sql base.

    Im stuck on the part where i am supposed to retrieve the information. I continue to get the error: java.net.UnkownHostException

    Code (Text):
    1. package lol.lol.lol;
    2. import java.io.BufferedReader;
    3. import java.io.InputStream;
    4. import java.io.InputStreamReader;
    5.  
    6. import org.apache.http.HttpEntity;
    7. import org.apache.http.HttpResponse;
    8. import org.apache.http.client.HttpClient;
    9. import org.apache.http.client.methods.HttpPost;
    10. import org.apache.http.impl.client.DefaultHttpClient;
    11. import org.json.JSONArray;
    12. import org.json.JSONObject;
    13.  
    14. import android.app.Activity;
    15. import android.os.Bundle;
    16. import android.util.Log;
    17. import android.widget.TextView;
    18.  
    19. public class lol extends Activity {
    20.     /** Called when the activity is first created. */
    21.     public int num;
    22.     public void onCreate(Bundle savedInstanceState) {
    23.         super.onCreate(savedInstanceState);
    24.         setContentView(R.layout.main);
    25.         TextView txt = (TextView)this.findViewById(R.id.text);
    26.         txt.setText(getScramble("lol",0));
    27.     }
    28.     public String getScramble(String table, int numb){
    29.         HttpClient client = new DefaultHttpClient();
    30.         HttpPost post = new HttpPost("http://www.youtube.com");
    31.         HttpResponse response = null;
    32.         HttpEntity entity = null;
    33.         InputStream is = null;
    34.         BufferedReader reader = null;
    35.         String s = "";
    36.         try{
    37.             response = client.execute(post);
    38.         }catch(Exception e){
    39.             Log.e("log_tag",e.toString()+": "+e.getMessage());
    40.             return e.toString(); [B][COLOR="Red"]<---- So far this is where it throws the exception[/COLOR][/B]
    41.         }
    42.         try{
    43.             entity = response.getEntity();
    44.         }catch(Exception e){
    45.             Log.e("log_tag",e.toString()+": "+e.getMessage());
    46.             return e.getMessage();
    47.         }
    48.         try{
    49.             is = entity.getContent();
    50.         }catch(Exception e){
    51.             Log.e("log_tag",e.toString()+": "+e.getMessage());
    52.             return e.getMessage();
    53.         }
    54.         try{
    55.             reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
    56.         }catch(Exception e){
    57.             Log.e("log_tag",e.toString()+": "+e.getMessage());
    58.             return e.getMessage();
    59.         }
    60.         StringBuilder sb = new StringBuilder();
    61.         String line = null;
    62.         try{
    63.             while ((line = reader.readLine()) != null) {
    64.                 sb.append(line + "\n");
    65.             }
    66.             is.close();
    67.         }catch(Exception e){
    68.             Log.e("log_tag",e.toString()+": "+e.getMessage());
    69.             return e.getMessage();
    70.         }
    71.  
    72.         String result = sb.toString();
    73.         try{
    74.             JSONArray jArray = new JSONArray(result);
    75.             for(int i=0;i<jArray.length();i++){
    76.                 JSONObject json_data = jArray.getJSONObject(i);
    77.                 Log.i("log_tag","2x2x2: "+json_data.getString("two2H")+
    78.                         ", 3x3x3: "+json_data.getString("three2H"));
    79.                 s = json_data.getString("two2H");
    80.             }
    81.         }catch(Exception e){
    82.                 Log.e("log_tag",e.toString()+": "+e.getMessage());
    83.                 return e.getMessage();
    84.             }
    85.         return s;      
    86.     }
    87. }
    88.  
    anything that could potentially help me would be awesome, Thanks in advance

    Advertisement
  2. jms_gears1

    jms_gears1 Member

    can no one help me?
    Do i need more information, or is it just because im using the eumlator?
  3. o90pO()P

    o90pO()P New Member

    I had the same problem until i put
    Code (Text):
    1. <uses-permission android:name="android.permission.INTERNET" />
    in the AndriodManifest.xml as the first child element (i.e. even before the application element). Then it worked.
  4. swiftk77

    swiftk77 New Member

    Thank you, I missed the permissions setting and spend a couple hours beating my head against the wall.

    Thanks!
  5. guerrarj

    guerrarj New Member

Share This Page