0

I want to build an android application to send an image data over a server. To start with, I wanted to send the text data from my app to localhost on my machine. I am using a WAMP server.

I have written this code on eclipse and also created mypage.php on C:/wamp/www/ folder

package com.example.httpexample;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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 android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://localhost/mypage.php");
        try {
             List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);

             nameValuePairs.add(new BasicNameValuePair("fname", "vinod"));
             nameValuePairs.add(new BasicNameValuePair("fphone", "1234567890"));
             nameValuePairs.add(new BasicNameValuePair("femail", "abc@gmail.com"));
             nameValuePairs.add(new BasicNameValuePair("fcomment", "Help"));
             httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
             httpclient.execute(httppost);

         } catch (ClientProtocolException e) {
             // TODO Auto-generated catch block
         } catch (IOException e) {
             // TODO Auto-generated catch block
         }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

But when I run the emulator I can't see that the data is sent from the emulator to mytest.php file on the localhost.

I am new in this domain and probably not doing some things right. Please suggest a way out. Ask me for more details if you want to. Also I would like to tell that my institute uses a proxy server( but since I am connecting to my machine shouldnt be a problem).

Rahul Katare
  • 115
  • 1
  • 2
  • 7
  • please make all Network Stuff in a AsyncTask 6.5 at this tut http://www.vogella.com/articles/AndroidBackgroundProcessing/article.html – A.S. Oct 17 '13 at 09:50
  • Also have a look at http://stackoverflow.com/questions/5806220/how-to-connect-to-my-http-localhost-web-server-from-android-emulator-in-eclips – A.S. Oct 17 '13 at 09:51

1 Answers1

1

Use multipart entity to upload image data on server or local link for upload image and follow this link to connect localhost.

Community
  • 1
  • 1
Harshit Rathi
  • 1,862
  • 2
  • 18
  • 25
  • yes I am now able to send data to my local host. Will follow the link to get the image part done and get back to you. – Rahul Katare Oct 17 '13 at 10:12