开发者

Http request using android requesting for a php page in local host

开发者 https://www.devze.com 2023-02-27 08:21 出处:网络
I am creating an android application that needs to interact with a local web server(WAMP). WAMP has a php file that just echos Hello world. This application requests for this page but when I run the a

I am creating an android application that needs to interact with a local web server(WAMP). WAMP has a php file that just echos Hello world. This application requests for this page but when I run the application and click on the button that fires the request event, there are no results printed on the EditText saying Helloworld. The following is the code:

package com.mobilevoter;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLConnection;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class mobilevoter extends Activity implements OnClickListener {
    Button proceed;
    EditText voter_number;
    EditText id_number;
    URLConnection conn;
    BufferedReader myreader;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        proceed=(Button)findViewById(R.id.proceed_btn);
        voter_number=(EditText)findViewById(R.id.editText_voternumber);
        id_number=(EditText)findViewById(R.id.editText_idnumber);
        proceed.setOnClickListener(this);
    }
    @Override
    public void onClick(View src) {

            if(src==proceed){
                HttpClient authenticationClient= new DefaultHttpClient();
                HttpPost url_post=new HttpPost("http://10.0.2.2/practice/index.php");
                HttpResponse response;
                try
                {
                response=authenticationClient.execute(url_post);
                HttpEntity authenticationEntity=response.getEntity();
                InputStream authenticationStream=authenticationEntity.getContent();
                BufferedReader myreader=new BufferedReader(new InputStreamReader(authenticationStream));
                String inputStream;
                wh开发者_C百科ile((inputStream=myreader.readLine())!=null)
                {
                    voter_number.setText(inputStream);
                }
                }
                catch(Exception exception)
                {
                    System.out.println(exception.getMessage());
                }
                /*finally
                {
                    try
                    {
                    myreader.close();
                    }
                    catch(IOException ioexception)
                    {
                        System.out.println(ioexception.getMessage());
                    }
                }*/

            }
    }
}

The manifest looks like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.mobilevoter"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".mobilevoter"
                  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>
    <uses-permission android:name="android.permission.INTERNET" />
</manifest>

I will really appreciate the help. Thanx.


Can you reach that URL from the browser app built-in the mobile/emulator?

If not, you need to check your network setup. 10.0.2.2 seems to be ok from what the guide says. Perhaps there is a firewall? Does the WAMP server listen on port 80?


http://progrium.com/localtunnel/

$ gem install localtunnel
$ localtunnel 8000 

share this url: 
http://xyz.localtunnel.com

You'll need RubyGems installed, but this is the easiest solution for me.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号