0

I've been trying this for the best part of two weeks now, and I am really stuck. Initially I had created a simple ObjectOutputStream client - server program - with the client being the Android app, but it does not work (it reads the connection but not the object).

So now I am confused as to what other approaches I might be able to take to carry out this simple task? Can anyone Help?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
BodhiByte
  • 209
  • 2
  • 8
  • 16
  • Are there any requirements on the server? Otherwise I'd consider going for an HTTP server and use that protocol to send data back and forth. – claesv Feb 23 '12 at 16:01

4 Answers4

0

have you tried URLConnection using post method? :)

Or get method like:

String yourURL = "www.yourwebserver.com?value1=one&value2=two";
URL url = new URL(yourURL);
URLConnection connection = url.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
response = in.readLine();
Jayyrus
  • 12,961
  • 41
  • 132
  • 214
0

you can try JSON stirng to send data. We have a lot of stuff available on how to work with JSON and also there are many api's. JSONSimple is the one I can suggest. Its really easy.

D3stiny
  • 13
  • 2
  • I have been looking at JSON but have failed to find a tutorial that I can understand, they all seem really convoluted. – BodhiByte Feb 23 '12 at 16:14
  • If you want to serialize a java object to json. I'd suggest Gson https://sites.google.com/site/gson/gson-user-guide. This is really good, especially when deserializing from json to java object. – Cuong Thai Feb 23 '12 at 16:23
0

why don't you try this:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Pedro Teran
  • 1,200
  • 3
  • 17
  • 43
  • 1
    also I recomend Json for parsing data – Pedro Teran Feb 23 '12 at 16:05
  • So I can post an Object(name value pairs) to a server and read it from a servlets? Can this work a with a hash table? – BodhiByte Feb 23 '12 at 16:15
  • If your object data just have a couple of fields such as ProductID, Price, then, I think you can send them as a key value pairs. However, if you have a lot of fields, I believe using json and let the library take care reconstruct the object on server for you. :) – Cuong Thai Feb 23 '12 at 16:27
0

You can use this to post an Entity to server:

    HttpClient httpClient = new DefaultHttpClient();
    HttpPost postRequest = new HttpPost(url);
    postRequest.setEntity(entity);
    try {

        HttpResponse response = httpClient.execute(postRequest
                );
        String jsonString = EntityUtils.toString(response
                .getEntity());
        Log.v(ProgramConstants.TAG, "after uploading file "
                + jsonString);
        return jsonString;

    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

An Entity can be name value pair:

List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("key1", value1));
nvps.add(new BasicNameValuePair("key2", value2));
Entity entity=new UrlEncodedFormEntity(nvps, HTTP.UTF_8)  

Or you can send an entity with bytearray.

Bitmap bitmapOrg=getBitmapResource();
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);

byte[] data = bao.toByteArray();

MultipartEntity entity=new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE)
entity.addPart("file", new ByteArrayBody(data, "image/jpeg",
                        "file"));

If you want to post json to server:
Please check out this link How do I send JSon as BODY In a POST request to server from an Android application?
For serializing and deserializing java object, I recommend https://sites.google.com/site/gson/gson-user-guide#TOC-Using-Gson Really hope it can help you see an overview of sending data to server

Community
  • 1
  • 1
Cuong Thai
  • 1,165
  • 1
  • 11
  • 24
  • thanks i am going to look into posts to servers and attempt to write a servlet. Can a hash table be an entity also? – BodhiByte Feb 23 '12 at 16:18
  • Yes, you can build an NameValuePair object from your hashtable. Then construct UrlEncodedFormEntity with that NameValuePair object – Cuong Thai Feb 23 '12 at 16:21