1

I want to send some information from PHP to Java. Why? Because I have a database on my server, and I get information from my database using PHP scripts.

Now I want to send that information to my client in Java. How can I do that?

I send information from Java to PHP by POST, and it works well, but I don't know how can I do the reverse.

Can you help me?

I saw this code, from a GET connection in Java... is it correct?

public String HttpClientTutorial(){
    String url = "http://testes.neoscopio.com/myrepo/send.php";
       InputStream content = null;  
       try {  
         HttpClient httpclient = new DefaultHttpClient();  
         HttpResponse response = httpclient.execute(new HttpGet(url)); 

         content = response.getEntity().getContent();  
       } catch (Exception e) {  
         Log.e("[GET REQUEST]", "Network exception", e);  
       }  


  String response = content.toString();

  return response;
}

P.S: I'm an android developer, not a Java developer...

2
  • what do u mean by android developer ?? Commented May 25, 2010 at 11:57
  • 1
    So are you saying that you want the Java part to act as a server that the PHP connects to, or do you mean you want to respond to that POST call that the Java client sends to you PHP? Commented May 25, 2010 at 12:03

1 Answer 1

2

From exampledepot: Sending POST request (Modified to get the output of your send.php.)

try {
    // Construct data
    String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
    data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");

    // Send data
    URL url = new URL("http://testes.neoscopio.com/myrepo/send.php");
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(data);
    wr.flush();

    // Get the response
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
        // Process line...
    }
    wr.close();
    rd.close();
} catch (Exception e) {
}

P.S. This should work fine on Android. :)

(I usually import static java.net.URLEncoder.encode but that's a matter of taste.)

Sign up to request clarification or add additional context in comments.

4 Comments

The code at exampledepot obviously didn't use testes.neoscopio.com, that's what I changed :-)
of course.. but in file send.php How do I send the information to the java? this code will get something to the php right?
i have 1 more question.. it's possible send information by the 'GET' connection ? like.. a send a string to send.php.. and receive the same output..
Sure. Change the URL to send.php?yourVariable=aValue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.