0

I have following problem: I'm developing the application which need to authorize on server and upload data from my mobile into it. The server side is ready and works correctly. So, for authorizing I use the following code:

URL url = new URL(VALIDATING_URL);
                URLConnection connection=url.openConnection();
                connection.setDoOutput(true);
                PrintWriter out=new PrintWriter(connection.getOutputStream());
                out.print(POST_QUERY_EMAIL+email);
                out.print("&");
                out.print(POST_QUERY_PASS+password);
                out.print("&");
                out.print(POST_QUERY_CHANNEL+channel);
                out.close();
                Scanner in=new Scanner(connection.getInputStream());
                StringBuilder result=new StringBuilder();
                while (in.hasNextLine()) {
                    result.append(in.nextLine());
                    result.append("\n");
                }
                in.close();

It works correctly, and the application will get needed result if I enter correctly data. So, now I need to upload data into server using POST-query, but I don't know how I can do it. Using HTML forms, video is usually uploaded using 'userfile' variable and will be got from $_FILES array in PHP scipts. How can I upload do it from Java? Can I just print data into PrintStream from InputStream?

Thank you, I hope you can help me

user1445877
  • 83
  • 1
  • 8

2 Answers2

1

Try this,

public void postData() throws Exception {


 HttpClient client = new DefaultHttpClient();
 HttpPost httppost = new HttpPost("https://www.xyz.com");

 List<NameValuePair> list = new ArrayList<NameValuePair>(1);

 list.add(new BasicNameValuePair("name","ABC");

 httppost.setEntity(new UrlEncodedFormEntity(list));

 HttpResponse r = client.execute(httppost);

}
Kumar Vivek Mitra
  • 32,278
  • 6
  • 43
  • 74
0

I would suggest reading this. It shows you how to do a POST with URLConnection and explains what's going on.

Community
  • 1
  • 1
Eliezer
  • 6,606
  • 9
  • 54
  • 100