添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm trying to convert this Python code using the Python Requests HTTP library into Java code (for Android).

import requests
payload = {"attr[val1]":123,
           "attr[val2]":456,
           "time":0,
           "name":"Foo","surname":"Bar"}
r = requests.post("http://jakiro.herokuapp.com/api", data=payload)
r.status_code
r.text

This is what I've done so far:

protected void sendJson() {
    Thread t = new Thread() {
        public void run() {
            Looper.prepare(); //For Preparing Message Pool for the child Thread
            HttpClient client = new DefaultHttpClient();
            HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
            HttpResponse response;
            JSONObject json = new JSONObject();
            try {
                Log.v("SOMETHING_NAME3", "Creating POST");
                HttpPost post = new HttpPost("http://jakiro.herokuapp.com/api");
                json.put(MessageAttribute.SURNAME, "Bar");
                json.put(MessageAttribute.VAL1, 123);
                json.put(MessageAttribute.VAL2, 456);
                json.put(MessageAttribute.name, "Foo");
                json.put(MessageAttribute.TIME, 0);
                StringEntity se = new StringEntity( json.toString() );
                se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                post.setEntity(se);
                response = client.execute(post);
                /*Checking response */
                if(response!=null){
                    InputStream in = response.getEntity().getContent(); //Get the data in the entity
                    String foo = convertStreamToString(in);
                    Log.v("SOMETHING_NAME2", foo); // Gives me "Bad request"
            } catch(Exception e) {
                e.printStackTrace();
                Log.v("SOMETHING_NAME", "Cannot Establish Connection");
            Looper.loop(); //Loop in the message queue
    t.start();

I've checked the response with response.getStatusLine().getStatusCode() and I get back a 400 from the server. The Python code works fine, but in Java on an Android device it doesn't. I can't see to figure out why though.

No clue, I don't work with Java. I'd imagine something like this: stackoverflow.com/questions/8120220/… – Blender Mar 10, 2014 at 11:24 @Blender Great, I got it working! If you could convert your comment to an answer, I'll gladly accept it. – jonalmeida Mar 10, 2014 at 17:37

@Blender's link was the correct solution at the link: How to use parameters with HttpPost

The correct way to url-encode was to create BasicNameValuePairs and encode that as such:

postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("param1", "param1_value"));
postParameters.add(new BasicNameValuePair("param2", "param2_value"));
httppost.setEntity(new UrlEncodedFormEntity(postParameters));
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.