D
D
Dmitry Richter2014-11-16 19:51:14
Android
Dmitry Richter, 2014-11-16 19:51:14

How to send a POST request on an Android device?

Good day. I need to send a POST request to the server and everything seems to be clear, but the problem is that all the data that was sent to me for it is just this picture:
06e0c5d3dfe84843b8d35a616d06fda4.jpg
I first tried to implement it in the usual way:

HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("http://televizera.net/sms/");
        try {
            StringEntity stringEntity = new StringEntity("act=createOrder&phone=79154508472&place=1&pays=1m2");
           post.setEntity(stringEntity);
            post.setHeader("Content-type", "application/xhtml+xml");
  HttpResponse response = client.execute(post);
            BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            line = "";
            while ((line = rd.readLine()) != null) {
                line += line;
            }

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

But after it turned out that the application just crashes, after I tried to make all the parameters as a key:
HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("http://televizera.net/sms/");
     try {List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair(4);
            nameValuePairs.add(new BasicNameValuePair("act","createOrder"));
            nameValuePairs.add(new BasicNameValuePair("phone","06620"));
            nameValuePairs.add(new BasicNameValuePair("place","1"));
            nameValuePairs.add(new BasicNameValuePair("pays","1m2"));
            post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
 HttpResponse response = client.execute(post);
            BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            line = "";
            while ((line = rd.readLine()) != null) {
                line += line;
            }

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

But it also turned out that the application behaves the same way. The parameters are all entered correctly, but the request still does not go through, I can’t understand why, like 1 method was correct, but still it doesn’t work, and the server does not try to return anything, but on The stage of sending the request itself just crashes from the application. Tell me what could be wrong in my code or what could be the problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2014-11-16
@itvdonsk

What error does it give on exit? I suspect that "NetworkOnMainThreadException"

E
Error_403_Forbidden, 2014-11-19
@Error_403_Forbidden

Did you forget to put it in the manifest?
uses-permission android:name="android.permission.INTERNET"
uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question