android httpclient.execute在模拟器上抛出异常

时间:2012-02-19 12:35:28

标签: java android httpclient http-post

我正在尝试使用httppost连接到网页。但是下面的代码从httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));开始抛出异常。

我已尝试过所有组合,但无法找到问题。我该怎么调试呢?我是android + java世界的新手。

修改 为了避免任何问题,我只是按照这里给出的例子进行操作。

http://www.vogella.de/articles/AndroidNetworking/article.html

在第4部分。但它仍然给出同样的错误。任何人都可以帮忙吗? 谢谢。

修改结束

package com.example.row;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpEntity;
import java.io.InputStream;
import android.widget.TextView;
import org.apache.http.util.EntityUtils;


public class mcrow3 extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        String result = "MC";
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
        nameValuePairs.add(new BasicNameValuePair("myusername","user"));
        nameValuePairs.add(new BasicNameValuePair("mypassword","pwd"));
        TextView tv = new TextView(this);
        try
        {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://example.com/login.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        result = EntityUtils.toString(entity);
        }
        catch(Exception e)
        {
            tv.setText("Some problem");
            setContentView(tv);
        }

        tv.setText(result);
        setContentView(tv);
    }
}

如果我做错了,请告诉我。在模拟器上安装它是说unfortunatley应用程序停止。我在模拟器上缺少任何配置吗?我的模拟器的互联网连接正在浏览器上运行。

修改 请找到服务器端PHP脚本:

<?php

$host="host"; // Host name
$username="user"; // Mysql username
$password="pwd"; // Mysql password
$db_name="db"; // Database name
$tbl_name="users"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE emailid='$myusername' and password='$mypassword'";
while($e=mysql_fetch_assoc($sql))
        $output[]=$e;

print(json_encode($output));
mysql_close();
?>

2 个答案:

答案 0 :(得分:1)

虽然httppost实体设置为URLEncodedFormEntity,但您可能还需要在http标头内容类型中指定为:Content-Type:application / x-www-form-urlencoded。

也可能是因为你在与UI相同的线程上进行网络调用。

答案 1 :(得分:0)

设置您的参数并发布实体,如下所示:

String jsonParam = null;
try{
    JSONObject param = new JSONObject();
    param.put("myusername", "user");
    param.put("mypassword", "pwd");
    //and so on with other parameters

    jsonParam = param.toString();
}
catch (Exception e) {
    // TODO: handle exception
}

httppost.setEntity(new StringEntity(jsonParam, HTTP.UTF_8));