2012-05-04 4 views
3

でリクエストを受け取ります。HTTP GETリクエストを送信するのに手伝いが必要です。私のコードは次の通りです:HttpはAndroid 2.3.3

URL connectURL = new URL("url"); 
    HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection(); 

    conn.setDoInput(true); 
    conn.setDoOutput(true); 
    conn.setUseCaches(false); 
    conn.setRequestMethod("GET"); 

    conn.connect(); 
    conn.getOutputStream().flush();  
    String response = getResponse(conn); 

しかし、それはgetResponse(conn);で失敗します。なぜですか?

+0

'getResponse'はどこから来ましたか(このコードはどのクラスに置かれていますか?)、エラーは何ですか? – jadkik94

答えて

15

GETリクエストは次のように使用することができます:

try { 
    HttpClient client = new DefaultHttpClient(); 
    String getURL = "http://www.google.com"; 
    HttpGet get = new HttpGet(getURL); 
    HttpResponse responseGet = client.execute(get); 
    HttpEntity resEntityGet = responseGet.getEntity(); 
    if (resEntityGet != null) { 
     // do something with the response 
     String response = EntityUtils.toString(resEntityGet); 
     Log.i("GET RESPONSE", response); 
    } 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
+0

私はパラメータを送信したい。方法は? – MaximumBua

+1

次のようなパラメータを追加することができます: "http://www.google.com?param1=value1¶m2=value2"また、データを送信するPOSTメソッドもあります。この場合、一般的なURLにパラメタを追加する必要はありませんより好ましい。 http://www.diffen.com/difference/Get_vs_Post – Caner

0

私はsetDoOutput(真)が自動的にPOSTを意味信じています。

0

私は私の側で正常に動作しているGET応答のためのコードのこの部分を使用しています:

HttpClient client = new DefaultHttpClient(); 
    HttpGet request = new HttpGet(requestUrl); 

    HttpResponse response=null; 
    try { 
     response = client.execute(request); 
    } catch (ClientProtocolException e) { 
     Log.d(TAG,"1. "+e.toString()); 

    } catch (IOException e) { 
     Log.d(TAG,"2. "+e.toString()); 

    } 
    int status_code=response.getStatusLine().getStatusCode(); 
    Log.d(TAG,"Response Code returned ="+status_code); 

    BufferedReader in=null; 
    try { 
     in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 
    } catch (IllegalStateException e) { 
     Log.d(TAG,"3. "+e.toString()); 
    } catch (IOException e) { 
     Log.d(TAG,"4. "+e.toString()); 
    } 

    StringBuffer sb = new StringBuffer(""); 
    String line = ""; 
    String newline = System.getProperty("line.separator"); 
    try { 
     while ((line = in.readLine()) !=null){ 
      sb.append(line + newline); 
     } 
     String data = sb.toString(); 
     Log.d(TAG,data); 
    } catch (IOException e) { 
     Log.d(TAG,"5. "+e.toString()); 
    } 
    try { 
     in.close(); 
    } catch (IOException e) { 
     Log.d(TAG,"6. "+e.toString()); 
    } 
    String data = sb.toString(); 

    try { 
     if(status_code==200 || status_code==401){ 
      JSONObject responseJSONObject = new JSONObject(data); 
      responseJSONObject.put("tpg_status_code", status_code); 
     } 
    } catch (JSONException e) { 
     Log.d(TAG,"6. "+e.toString()); 
    } 
0

私はこのコードを使用し、それが完璧に動作します:

public class HttpGetAndroidExample extends Activity { 

      TextView content; 
      EditText fname,email,login,pass; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 

        super.onCreate(savedInstanceState); 
       setContentView(R.layout.activity_http_get_android_example); 

       content = (TextView)findViewById(R.id.content); 
       fname  = (EditText)findViewById(R.id.name); 
       email  = (EditText)findViewById(R.id.email); 
       login  = (EditText)findViewById(R.id.loginname); 
       pass  = (EditText)findViewById(R.id.password); 

       Button saveme=(Button)findViewById(R.id.save); 


     saveme.setOnClickListener(new Button.OnClickListener(){ 
      public void onClick(View v) 
      { 
         //ALERT MESSAGE 
        Toast.makeText(getBaseContext(),"Please wait, connecting to server.",Toast.LENGTH_LONG).show(); 

      try{ 

       // URLEncode user defined data 

        String loginValue = URLEncoder.encode(login.getText().toString(), "UTF-8"); 
        String fnameValue = URLEncoder.encode(fname.getText().toString(), "UTF-8"); 
        String emailValue = URLEncoder.encode(email.getText().toString(), "UTF-8"); 
        String passValue = URLEncoder.encode(pass.getText().toString(), "UTF-8"); 

       // Create http cliient object to send request to server 

        HttpClient Client = new DefaultHttpClient(); 

       // Create URL string 

       String URL = "http://androidexample.com/media/webservice/httpget.php?user="+loginValue+"&name="+fnameValue+"&email="+emailValue+"&pass="+passValue; 

       //Log.i("httpget", URL); 

       try 
       { 
           String SetServerString = ""; 

          // Create Request to server and get response 

           HttpGet httpget = new HttpGet(URL); 
          ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
          SetServerString = Client.execute(httpget, responseHandler); 

           // Show response on activity 

          content.setText(SetServerString); 
       } 
       catch(Exception ex) 
        { 
         content.setText("Fail!"); 
        } 
      } 
      catch(UnsupportedEncodingException ex) 
      { 
        content.setText("Fail"); 
      }  
     } 
    }); 

} }

関連する問題