Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从Android应用程序将数据发布到PHP脚本_Java_Php_Android_Httpclient - Fatal编程技术网

Java 从Android应用程序将数据发布到PHP脚本

Java 从Android应用程序将数据发布到PHP脚本,java,php,android,httpclient,Java,Php,Android,Httpclient,我正在尝试从Android手机应用程序将数据发布到PHP脚本。这是我正在使用的代码 public class Upload extends Activity implements OnClickListener { EditText joke; Button upload; AlertDialog alertDialog = new AlertDialog.Builder(this).create(); @Override protected void onCreate(Bundle saved

我正在尝试从Android手机应用程序将数据发布到PHP脚本。这是我正在使用的代码

public class Upload extends Activity implements OnClickListener {
EditText joke;
Button upload;
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.upload);
}
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.bUpload:
        uploadJoke();
        break;
    }

}
private void uploadJoke() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://xxxx/telejoke.php");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("username", "test"));
        nameValuePairs.add(new BasicNameValuePair("joke", joke.getText().toString()));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

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

    } catch (ClientProtocolException e) {
        alertDialog.setTitle("Client Protocol Exception");
    } catch (IOException e) {
        alertDialog.setTitle("IOException");
    }
} 
公共类上载扩展活动实现OnClickListener{
编辑文本笑话;
按钮上传;
AlertDialog AlertDialog=新建AlertDialog.Builder(this.create();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.upload);
}
公共void onClick(视图v){
//TODO自动生成的方法存根
开关(v.getId()){
案例R.id.bUpload:
上传笑话();
打破
}
}
私有void上传笑话(){
//创建一个新的HttpClient和Post头
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://xxxx/telejoke.php");
试一试{
//添加您的数据
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“用户名”、“测试”);
添加(新的BasicNameValuePair(“笑话”,笑话.getText().toString());
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
HttpResponse response=httpclient.execute(httppost);
}捕获(客户端协议例外e){
alertDialog.setTitle(“客户端协议异常”);
}捕获(IOE异常){
alertDialog.setTitle(“IOException”);
}
} 
}


然而,当我尝试在我的android手机上运行它时,它只是因为意外错误而崩溃。感谢您的帮助。

移动
AlertDialog-AlertDialog=new-AlertDialog.Builder(this.create()
UploadChake
方法中修复了此问题。

你好,moesef,请发布错误日志,这样,我可以建议更改,Thxy您正在主线程中运行网络操作,我建议您在新线程或AsyncTask中移动网络操作,错误与AlertDialog有关。当我将AlertDialog移动到uploadJoke方法中时,它会删除错误。。。但是我仍然不认为数据被发布到我的PHP脚本中。事实上,AlertDialog是阻止它工作的错误。Thx的帮助@KPBird应该首先查看错误日志。