androboy
androboy

Reputation: 845

android httpclient.execute throwing exception on emulator

I am trying to connect to a webpage with httppost. But the below code is throwing exception starting from httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));.

I have tried all combinations, but not able to find problem with it. How can I debug it? I am fairly new to the android + java world.

Edit: In order to avoid any kind of issues, I simply followed the example given here..

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

in the 4th section. But it still is giving the same error. Can any one please help? Thanks.

Edit End

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);
    }
}

Please let me know if I am doing some thing wrong. On installing it on the emulator it is saying unfortunatley app stopped. Am I missing any configuration on emulator? My emulator's internet connection is working on the browser.

Edit: Please find the server side PHP script:

<?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();
?>

Upvotes: 1

Views: 2014

Answers (2)

user1050134
user1050134

Reputation: 582

Though httppost entity is set as URLEncodedFormEntity, may be you also need to specify in http header content type as: Content-Type: application/x-www-form-urlencoded .

And also could it be because you are doing network call on the same thread as that of UI.

Upvotes: 1

waqaslam
waqaslam

Reputation: 68187

set your parameters and post entity as below:

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));

Upvotes: 0

Related Questions