Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8289535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:31:46+00:00 2026-06-08T12:31:46+00:00

Im trying to send an JSONObject over HTTPPost, however now Im receiving a 400

  • 0

Im trying to send an JSONObject over HTTPPost, however now Im receiving a 400 error.

I’ve been going through with the server coder, and making smaller and more concise calls with a less complicated JSON object, however I still receive the same 400 error.

How do I go about fixing it?

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginScreen extends Activity {

    private Button loginButton;
    private TextView resultText;
    private EditText usernameText;
    private EditText passwordText;
    private EditText switchChoice;
    private EditText installText;
    private int i;
    private String accessURL = "URL";
    private String accessNEW = "";
    public final String CONSUMER_KEY = "KEY";
    public final String CONSUMER_SECRET = "SECRET";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // Create the screen + pull back the saved instance state
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_screen);

        // initialize the buttons and text view for usability in the later codes
        loginButton = (Button) findViewById(R.id.btn_login);
        resultText = (TextView) findViewById(R.id.lbl_result);
        usernameText = (EditText) findViewById(R.id.txt_username);
        passwordText = (EditText) findViewById(R.id.txt_password);
        installText = (EditText) findViewById(R.id.txt_install);
        switchChoice = (EditText) findViewById(R.id.txt_switch);



        // create the onclick listener for the login button to start the code to
        // login
        loginButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {
                    // pull data from EditText Boxes and create the JSON object
                    // to send information HTTP
                    String text = switchChoice.getText().toString();
                    i = Integer.parseInt(text);
                    JSONObject jOb = toJSON();
                    Log.d("JSONObjectCreation", jOb.toString());
                    // method to go through
                    processURL(jOb);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.login_screen, menu);
        return true;
    }

    public JSONObject toJSON() throws JSONException {
        JSONObject credentials = new JSONObject();
        try {
            credentials.put("ConsumerSecret", CONSUMER_SECRET);
            credentials.put("ConsumerKey", CONSUMER_KEY);
            credentials.put("Password", "Sh0wT1me");
            credentials.put("Username", "sjones");

        } finally {
        }
        return credentials;
    }

    /*
     * Subclass that executes the connection and authentication to the server
     * using HTTPPost - Uses AsyncTask to execute the network connection on a
     * different thread
     * 
     * PARAMS: JSONObject -> input
     */
    private class PostTask extends AsyncTask<JSONObject, Integer, String> {
        // Dialog box to let user know that it is processing
        private ProgressDialog Dialog = new ProgressDialog(LoginScreen.this);

        // Before the execution of the background task, this is executed in the
        // main thread
        @Override
        protected void onPreExecute() {
            Dialog.setMessage("Logging In...");

            // forces the dialog to show in the main thread
            Dialog.show();
        }

        // done in background thread, so that the main thread is not jammed up
        // preventing user interface usage
        @Override
        protected String doInBackground(JSONObject... jObj) {
            int TIMEOUT_MILLISEC = 10000; // create timeout time of 10 seconds
            HttpParams httpParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParams,
                    TIMEOUT_MILLISEC);
            HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);

            // create a new httpClient with the httpParams of timeout time
            HttpClient client = new DefaultHttpClient(httpParams);
            String responseBody = null;
            try {

                HttpPost request = new HttpPost(accessURL); 

                switch (i) {
                case 1:
                    request.setEntity(new ByteArrayEntity(jObj.toString().getBytes("UTF8")));
                    Log.d("request_entity", request.getEntity().getContent().toString());
                case 2:
                     StringEntity params =new StringEntity("{credentials"+jObj.toString()+"}");
                        Log.i("String_Entity", params.toString());

                     request.addHeader("content-type", "application/json; charset=utf-8");
                     request.setEntity(params);
                case 3:
                    StringEntity se = new StringEntity("{credentials" + jObj.toString()+"}");  
                    Log.i("String_Entity", se.toString());
                    se.setContentEncoding("UTF-8");
                    se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                    request.setEntity(se);
                case 4:

                }

                HttpResponse response = client.execute(request);
                int status = response.getStatusLine().getStatusCode();
                /*
                  switch (status) {
                    case 200:

                    HttpEntity entity = response.getEntity();
                    if (entity != null) {
                         responseBody = EntityUtils.toString(entity);
                    }
                    break;
                case 500:
                    responseBody = "500" + response.getStatusLine().getReasonPhrase();
                    break;
                }
                */
                String statusS = Integer.toString(status);
                Log.d("request_status", statusS);
                return statusS + " " +response.getStatusLine().getReasonPhrase();
                //return responseBody;
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                Log.e("log_tag", "Error in http connection " + e.toString());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            resultText.setText(result);
            Dialog.dismiss();
            Toast.makeText(getApplicationContext(), "Value updated",
                    Toast.LENGTH_SHORT).show();
        }
    }

    public void processURL(JSONObject thing) {
        // execute connection on new thread
        new PostTask().execute(thing);
    }

}

EDIT:
The switch part for setting the entity is there because I wasnt sure if the error is from the manner in which I was setting the entity, so after research, I created a switch to let me go through the different methods that the JSONObject can be encoded to the entity.

EDIT:
This came up in my logcat today:

07-19 11:13:27.812: W/ThrottleService(91): unable to find stats for iface rmnet0
07-19 11:15:18.862: A/NetworkStats(91): problem reading network stats
07-19 11:15:18.862: A/NetworkStats(91): java.lang.IllegalStateException: problem parsing line: null
07-19 11:15:18.862: A/NetworkStats(91):     at com.android.internal.net.NetworkStatsFactory.readNetworkStatsDetail(NetworkStatsFactory.java:313)
07-19 11:15:18.862: A/NetworkStats(91):     at com.android.server.NetworkManagementService.getNetworkStatsUidDetail(NetworkManagementService.java:1271)
07-19 11:15:18.862: A/NetworkStats(91):     at com.android.server.net.NetworkStatsService.performPollLocked(NetworkStatsService.java:810)
07-19 11:15:18.862: A/NetworkStats(91):     at com.android.server.net.NetworkStatsService.performPoll(NetworkStatsService.java:771)
07-19 11:15:18.862: A/NetworkStats(91):     at com.android.server.net.NetworkStatsService.access$100(NetworkStatsService.java:128)
07-19 11:15:18.862: A/NetworkStats(91):     at com.android.server.net.NetworkStatsService$3.onReceive(NetworkStatsService.java:610)
07-19 11:15:18.862: A/NetworkStats(91):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:728)
07-19 11:15:18.862: A/NetworkStats(91):     at android.os.Handler.handleCallback(Handler.java:605)
07-19 11:15:18.862: A/NetworkStats(91):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-19 11:15:18.862: A/NetworkStats(91):     at android.os.Looper.loop(Looper.java:137)
07-19 11:15:18.862: A/NetworkStats(91):     at android.os.HandlerThread.run(HandlerThread.java:60)
07-19 11:15:18.862: A/NetworkStats(91): Caused by: java.io.FileNotFoundException: /proc/net/xt_qtaguid/stats: open failed: ENOENT (No such file or directory)
07-19 11:15:18.862: A/NetworkStats(91):     at libcore.io.IoBridge.open(IoBridge.java:406)
07-19 11:15:18.862: A/NetworkStats(91):     at java.io.FileInputStream.<init>(FileInputStream.java:78)
07-19 11:15:18.862: A/NetworkStats(91):     at java.io.FileReader.<init>(FileReader.java:42)
07-19 11:15:18.862: A/NetworkStats(91):     at com.android.internal.net.NetworkStatsFactory.readNetworkStatsDetail(NetworkStatsFactory.java:272)
07-19 11:15:18.862: A/NetworkStats(91):     ... 10 more
07-19 11:15:18.862: A/NetworkStats(91): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
07-19 11:15:18.862: A/NetworkStats(91):     at libcore.io.Posix.open(Native Method)
07-19 11:15:18.862: A/NetworkStats(91):     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:98)
07-19 11:15:18.862: A/NetworkStats(91):     at libcore.io.IoBridge.open(IoBridge.java:390)
07-19 11:15:18.862: A/NetworkStats(91):     ... 13 more
: E/(): Device disconnected
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T12:31:47+00:00Added an answer on June 8, 2026 at 12:31 pm

    I realized after some time the error came from the jObj variable in the doInBackground method.
    I’m going to try to describe the error to the best of my abilities, and from what I have seen:
    protected String doInBackground(JSONObject… jObj)
    The JSONObject variable isn’t actually a JSONObject because of the ‘…’, so when I used jObj.toString() it returned some weird sort of non-JSONObject looking string, almost like it wasn’t encoded. I would go back and past the string here, however I’ve already changed my code so much that I dont want to risk losing it.

    Thanks for everyone’s help!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I´m trying to send a image through tcp to a server, firts getting the
I'm trying to do a blackbery application that can send a JSONObject through a
Trying to send 3rd party scripts over an SSL or SPDY connection is a
Im trying to send ascii encoded message to a server. My problem is coming
Im trying to send simple login/password parameters to my PHP file in my server
I am trying to send data over to a cakephp (mvc) website, via $.post().
I'm currently trying to send data via POST to a server, and the server
I want to send JSON request through HTTPConnection but i am getting error when
Trying to send a html email via mail(), however gmail just displays the email
Im trying to send a pre-populated email using mailto and href however I soon

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.