2012-05-12 2 views
1

안녕하세요, 안드로이드 TextBox를 "http://www.weather.com"TextBox에 연결하려고합니까? 내 응용 프로그램 텍스트 상자에 입력 된 "문자열"을 웹 사이트 textBOx로 어떻게 전송할 수 있습니까? , AsyncTask를로 HTTP 요청 코드를 삽입Android 앱에서 웹 사이트 TextBox로 데이터를 입력하는 방법은 무엇입니까?

package in.niteesh.connectToServer; 

import java.io.IOException; 
import java.io.UnsupportedEncodingException; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.ParseException; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.entity.StringEntity; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.util.EntityUtils; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class ConnectServerActivity extends Activity 
{ 
    //private static final int LENGTH_SHORT = 0; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     //Take a Login Button 
     Button btnLogin = (Button)findViewById(R.id.btnLogin); 
     //Set the onClick Event 
     btnLogin.setOnClickListener(new View.OnClickListener() 
     { 
      public void onClick(View v) 
      { 

        //Get TextBox from the layout 
        EditText etUserName = (EditText)findViewById(R.id.etUser); 

        //Client to make the request to your weather.com 
        DefaultHttpClient myClient = new DefaultHttpClient(); 

        //This is where you put the information you're sending. 
        HttpPost postUserCredentials = new HttpPost(getString(R.string.LoginAddress)); //Http Post is used for sending SOAP requests 
        HttpEntity postParameters = null; // Extract the byte to a string form 
        try 
        { 
         postParameters = new StringEntity("u=" + etUserName.getText()); 
        } 
        catch (UnsupportedEncodingException e) 
        { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 

        postUserCredentials.setHeader("Content-type", "http://www.weather.com/"); 
        postUserCredentials.setEntity(postParameters); 

        HttpResponse postResponse = null; 
        try 
        { 
         postResponse = myClient.execute(postUserCredentials); 
        } 
        catch (ClientProtocolException e) 
        { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
        catch (IOException e) 
        { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
        HttpEntity postResponseEntity = postResponse.getEntity(); 

        try 
        { 
         String result = EntityUtils.toString(postResponseEntity); 

        } 
        catch (ParseException e) 
        { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
        catch (IOException e) 
        { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
      } 
     }); 

    } 
} 

main.xml에 코드 모든

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <TextView android:id="@+id/tvUser" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Username:" /> 
    <EditText android:id="@+id/etUser" android:layout_width="match_parent" android:layout_height="wrap_content" /> 
    <Button android:id="@+id/btnLogin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Login" /> 
</LinearLayout> 

답변

2

첫째 : 여기

내가 작성한 코드입니다. 그렇지 않으면 요청이 기본 UI 스레드를 차단합니다. 또한 모든 try/catch 문을 HTTP 요청과 관련된 모든 try/catch 블록에 넣으십시오.

HTTP 게시물 데이터 here을 전송하는 간단한 스 니펫을 찾을 수 있습니다.

코드에서

,이 라인은 잘못 제거해야합니다 당신은 POST 데이터에 대한 링크 된 페이지에 보여로 NameValue 쌍을 사용하는 경우

postUserCredentials.setHeader("Content-type", "http://www.weather.com/"); 

, 코드의 나머지 부분은 내가 생각 괜찮습니다 .

관련 문제