2012-03-18 15 views
1

次のコードをAndroidクラスのjavaクラスで使用しました。それ以外のものはすべて動作します。答えは常にnullです。HttpPostを使用してローカルサーバーからデータを取得する

public class WebviewActivity extends Activity { 
    /** Called when the activity is first created. */ 
EditText et; Editable ur; 
TextView tx;HttpResponse response;String x; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    tx=(TextView)findViewById(R.id.textView1); 
    et=(EditText)findViewById(R.id.editText1); 
    Button button = (Button)findViewById(R.id.button1); 
    button.setOnClickListener(send); 

} 
private OnClickListener send = new OnClickListener() { 
    public void onClick(View v) { 
    ur=et.getText(); 
     postData(); 
    tx.setText("ans:"+x); 

    }}; 

public void postData() { 
    // Create a new HttpClient and Post Header 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost("http://localhost/name.php"); 

    try { 
     // Add your data 
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); 
     nameValuePairs.add(new BasicNameValuePair("name", ur.toString())); 

     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

     // Execute HTTP Post Request 
     response = httpclient.execute(httppost); 
     Log.d("myapp", "response " + response.getEntity()); 
     x= EntityUtils.toString(response.getEntity()); 


    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} } 

とPHPで私はコード

 <?php 
    $name=$_POST["name"]; 
     echo $name; 
    ?> 

それはそれを動作させる方法任意のアイデア以下の使用?

+0

あなたは、デバイスまたはコンピュータにサーバーを持っていますか? – MByD

+0

はい私は..私はPHPを有効にしてIIS 7を使用しています..他のPHPアプリケーションはすべて正常に動作します –

答えて

0

エミュレータでlocalhostにIPアドレス10.0.2.2を使用する必要があります。

+0

philip:どのようにIISにデータを転送しますか? –

+0

ありがとうあなたはphilipの作業:) –

関連する問題