2013-07-31 100 views
1

我是新來的Android和我試圖從我的Android應用程序發佈數據到本地主機。雖然它沒有顯示任何錯誤,並且總是說發送了命令,但它不會寫入文件,也可能不會發送任何數據。任何人都可以發現問題,或者告訴我如何修復它?發佈數據到本地主機在Android應用程序

MainActivity.java

package com.example.register; 

import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 
import android.app.Activity; 
import android.opengl.Visibility; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ProgressBar; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends Activity implements OnClickListener{ 

private EditText value; 
private Button btn; 
private ProgressBar pb; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
value=(EditText)findViewById(R.id.editText1); 
btn=(Button)findViewById(R.id.button1); 
pb=(ProgressBar)findViewById(R.id.progressBar1); 
pb.setVisibility(View.GONE); 
btn.setOnClickListener(this); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
getMenuInflater().inflate(R.menu.main, menu); 
return true; 
} 

public void onClick(View v) { 
// TODO Auto-generated method stub 
if(value.getText().toString().length()<1){ 
// out of range 
Toast.makeText(this, "please enter something", Toast.LENGTH_LONG).show(); 
}else{ 
pb.setVisibility(View.VISIBLE); 
new MyAsyncTask().execute(value.getText().toString()); 
} 

} 

private class MyAsyncTask extends AsyncTask<String, Integer, Double>{ 

@Override 
protected Double doInBackground(String... params) { 
// TODO Auto-generated method stub 
postData(params[0]); 
return null; 
} 

protected void onPostExecute(Double result){ 
pb.setVisibility(View.GONE); 
Toast.makeText(getApplicationContext(), "command sent", Toast.LENGTH_LONG).show(); 
} 
protected void onProgressUpdate(Integer... progress){ 
pb.setProgress(progress[0]); 
} 

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

try { 
// Add your data 
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
nameValuePairs.add(new BasicNameValuePair("myHttpData", valueIWantToSend)); 
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

// Execute HTTP Post Request 
HttpResponse response = httpclient.execute(httppost); 

} catch (ClientProtocolException e) { 
// TODO Auto-generated catch block 
} catch (IOException e) { 
// TODO Auto-generated catch block 
} 
} 

} 
} 

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<TextView 
android:id="@+id/textView1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignParentTop="true" 
android:layout_centerHorizontal="true" 
android:layout_marginTop="20dp" 
android:text="Enter Something Below:" 
android:textAppearance="?android:attr/textAppearanceLarge" /> 

<EditText 
android:id="@+id/editText1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_below="@+id/textView1" 
android:layout_centerHorizontal="true" 
android:layout_marginTop="30dp" 
android:ems="10" 
android:hint="" 
> 

<requestFocus /> 
</EditText> 

<ProgressBar 
android:id="@+id/progressBar1" 
style="?android:attr/progressBarStyleLarge" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_below="@+id/editText1" 
android:layout_centerHorizontal="true" 
android:layout_marginTop="24dp" /> 

<Button 
android:id="@+id/button1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignLeft="@+id/textView1" 
android:layout_alignRight="@+id/editText1" 
android:layout_below="@+id/progressBar1" 
android:layout_marginTop="24dp" 
android:text="Submit" /> 

</RelativeLayout> 
+0

打印響應字符串?你到了那裏? – KOTIOS

回答

4

你必須使用10.0.2.2 IP地址與本地主機系統連接。

HttpPost httppost = new HttpPost("http://localhost/text.php"); 

應該

HttpPost httppost = new HttpPost("http://10.0.2.2/text.php"); 
+0

我有使用這個,但它仍然沒有發佈,其實我是新來的機器人,你能告訴我我在哪裏可以檢查什麼問題,因爲現在它不顯示螞蟻錯誤onclick,只是顯示命令發送 –

+0

@RohitGoel檢查您在'HttpResponse response = httpclient.execute(httppost);'中獲得的響應。 –

+0

你能告訴我如何打印迴應 –

2
  1. 使用IP地址,而不是本地主機。例如:

    HttpPost httppost = new HttpPost("http://192.168.0.10/mypage.php"); 
    
  2. 添加Internet權限在AndroidManifest.xml例如:

+0

以哪種方式是'192.168.0.10' localhost? –

+1

我的世界中localhost的IP地址是'127.0.0.1' ... –

+0

@MdKamruzzaman:你無法從真相中走出來。首先,這是MongoDB綁定地址的問題。其次,雖然機器的所有其他IP可能會更改,但localhost或127.0.0.1絕不會更改,因爲它們是以這種方式定義的。 –

相關問題