Php Web服务器未使用Asynchttp-localhost从android应用程序接收帖子
我正在尝试使用android应用程序向网页发送一个简单字符串,并让网页存储该字符串。我最近安装了BitnamiWAMP堆栈以使用PHP代码,并通过本地主机运行了我的PHP。我学习了一些教程,遇到了一些使用AsynchPost发布数据的示例。我的PHP代码从不返回字符串。我不知道从哪里开始调试我的应用程序,因为我的PHP代码和Android应用程序没有返回任何错误。有人对可能出现的问题有什么建议吗?如有任何帮助/建议,将不胜感激。我在Apache2.2服务器上运行php脚本,并使用android的模拟器发送数据Php Web服务器未使用Asynchttp-localhost从android应用程序接收帖子,php,android,apache,post,localhost,Php,Android,Apache,Post,Localhost,我正在尝试使用android应用程序向网页发送一个简单字符串,并让网页存储该字符串。我最近安装了BitnamiWAMP堆栈以使用PHP代码,并通过本地主机运行了我的PHP。我学习了一些教程,遇到了一些使用AsynchPost发布数据的示例。我的PHP代码从不返回字符串。我不知道从哪里开始调试我的应用程序,因为我的PHP代码和Android应用程序没有返回任何错误。有人对可能出现的问题有什么建议吗?如有任何帮助/建议,将不胜感激。我在Apache2.2服务器上运行php脚本,并使用android的
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.post.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
主要活动
package com.example.post;
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 org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
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.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.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.home, 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]);
Log.v("parameters = ", 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://10.0.2.2/hello.php");
HttpResponse response = null;
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("data", valueIWantToSend));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
response = httpclient.execute(httppost);
String res = EntityUtils.toString(response.getEntity());
Log.v("Response = ", res);
} catch (ClientProtocolException e) {
e.printStackTrace();
// TODO Auto-generated catch block
} catch (IOException e) {
e.printStackTrace();
// TODO Auto-generated catch block
}
//return response.toString();
}
}
}
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.post.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package com.example.post;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.List;
导入org.apache.http.HttpResponse;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.ClientProtocolException;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.message.BasicNameValuePair;
导入org.apache.http.util.EntityUtils;
导入android.app.Activity;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.Menu;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.ProgressBar;
导入android.widget.Toast;
公共类MainActivity扩展活动实现OnClickListener{
私有文本值;
专用按钮btn;
私人ProgressBar pb;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
值=(EditText)findViewById(R.id.editText1);
btn=(按钮)findViewById(R.id.button1);
pb=(ProgressBar)findViewById(R.id.progressBar1);
pb.setVisibility(View.GONE);
btn.setOnClickListener(此);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//getMenuInflater().充气(R.menu.home,menu);
返回true;
}
公共void onClick(视图v){
//TODO自动生成的方法存根
if(value.getText().toString().length()如果有人再次遇到这个问题,请修复它。请参阅更新的代码-出于某种原因,文件内容不起作用,php从未设置数据。fwrite是一个更好的选择,而ERROR\u LOG是另一种将某些内容写入文件的方法,尽管它使用时间戳写入。有任何错误吗(检查你的日志)?@hichris123没有错误:(这就是我感到困惑的原因。嗯……如果你访问模拟器,它会显示什么吗?@hichris123我会检查。我如何在模拟器上显示我的地址?试着进入浏览器并键入http://10.0.2.2:82
。
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.post.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>