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 9008487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:56:55+00:00 2026-06-16T01:56:55+00:00

I want to insert some data into a database using a http post request.

  • 0

I want to insert some data into a database using a http post request.
My post.php page is:

      if(trim($_GET['param1'])!="" && trim($_GET['param2'])!="" && trim($_GET['param3'])!="" && trim($_GET['param4'])!="" && trim($_GET['param5'])!="")
{
    $connectionInfo = array(  "UID" => $DB_USER,  "PWD" => $DB_PASS, "Database"=>$DB_DATABASE );
    $con = sqlsrv_connect($DB_SERVER,$connectionInfo);
    if(!$con)
    {
      die('Could not connect: ' . sqlsrv_errors());
    }
    $query="INSERT INTO Table (param1,param2,param3,param4,param5) VALUES (
            '".$_GET['param1']."',
            ".$_GET['param2'].",
            ".$_GET['param3'].",
            '".$_GET['param4']."',
            ".$_GET['param5'].")";
            [...]

and in order to send data, I append all the parameters to the url, like: http://<myip>/MySite/post.php?param2=10&param3=8&param1=2012-12-01 13:00:00&param4=Name&param5=80

and everything works fine, but when I try to send my data from my Android application I could not achive my target.
How can I pass data via url?
In Android I’ve tried:

    HttpPost httppost = new HttpPost("http://<Myip>/MySite/post.php");
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
    nameValuePairs.add(new BasicNameValuePair("param2", "10"));
    nameValuePairs.add(new BasicNameValuePair("param3", "98"));
    nameValuePairs.add(new BasicNameValuePair("param1", "2012-12-01 13:00:00"));
    nameValuePairs.add(new BasicNameValuePair("param4", "Name"));
    nameValuePairs.add(new BasicNameValuePair("param5", "50"));
    //httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
    httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));  

    // Execute HTTP Post Request
    HttpResponse response = httpclient.execute(httppost)

and with:

String urlParameters = "param1=2012-12-01 13:00:00&param2=b&param3=c...";
String request = "http://<Myip>/MySite/post.php";
URL url = new URL(request); 
HttpURLConnection connection = (HttpURLConnection) url.openConnection();           
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false); 
connection.setRequestMethod("POST"); 
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Length", ""  Integer.toString(urlParameters.getBytes().length));
connection.setUseCaches (false);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
connection.disconnect();

but I always get errors.
Using second method the exception is:

12-17 18:08:26.617: E/AndroidRuntime(4837): FATAL EXCEPTION: main
12-17 18:08:26.617: E/AndroidRuntime(4837): android.os.NetworkOnMainThreadException
12-17 18:08:26.617: E/AndroidRuntime(4837):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
12-17 18:08:26.617: E/AndroidRuntime(4837):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
12-17 18:08:26.617: E/AndroidRuntime(4837):     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
12-17 18:08:26.617: E/AndroidRuntime(4837):     at libcore.io.IoBridge.connect(IoBridge.java:112)
12-17 18:08:26.617: E/AndroidRuntime(4837):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
12-17 18:08:26.617: E/AndroidRuntime(4837):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
12-17 18:08:26.617: E/AndroidRuntime(4837):     at java.net.Socket.connect(Socket.java:842)
12-17 18:08:26.617: E/AndroidRuntime(4837):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:77)
12-17 18:08:26.617: E/AndroidRuntime(4837):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
12-17 18:08:26.617: E/AndroidRuntime(4837):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
12-17 18:08:26.617: E/AndroidRuntime(4837):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
12-17 18:08:26.617: E/AndroidRuntime(4837):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
12-17 18:08:26.617: E/AndroidRuntime(4837):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
12-17 18:08:26.617: E/AndroidRuntime(4837):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
12-17 18:08:26.617: E/AndroidRuntime(4837):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
12-17 18:08:26.617: E/AndroidRuntime(4837):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
12-17 18:08:26.617: E/AndroidRuntime(4837):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
12-17 18:08:26.617: E/AndroidRuntime(4837):     at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:188)
12-17 18:08:26.617: E/AndroidRuntime(4837):     at  com.example.applic.applic.postData(MyApp.java:197)
12-17 18:08:26.617: E/AndroidRuntime(4837):     at com.example.applic.applic$1.run(MyApp.java:127)
12-17 18:08:26.617: E/AndroidRuntime(4837):     at android.os.Handler.handleCallback(Handler.java:605)
12-17 18:08:26.617: E/AndroidRuntime(4837):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-17 18:08:26.617: E/AndroidRuntime(4837):     at android.os.Looper.loop(Looper.java:137)
12-17 18:08:26.617: E/AndroidRuntime(4837):     at android.app.ActivityThread.main(ActivityThread.java:4514)
12-17 18:08:26.617: E/AndroidRuntime(4837):     at java.lang.reflect.Method.invokeNative(Native Method)
12-17 18:08:26.617: E/AndroidRuntime(4837):     at java.lang.reflect.Method.invoke(Method.java:511)
12-17 18:08:26.617: E/AndroidRuntime(4837):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
12-17 18:08:26.617: E/AndroidRuntime(4837):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
12-17 18:08:26.617: E/AndroidRuntime(4837):     at dalvik.system.NativeStart.main(Native Method)

What can I do?

  • 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-16T01:56:56+00:00Added an answer on June 16, 2026 at 1:56 am

    NetworkOnMainThreadException :

    The exception that is thrown when an application attempts to perform a
    networking operation on its main thread.

    means you are trying to run network operation from UI Thread so put your all network related code inside AsyncTask‘s doInBackground method and use onPostExecute for updating UI when doInBackground method execution complete

    and if your are using API LEVEL 9 or GREATER FROM 9 then add StrictMode in onCreate of Activity to avoid this error

    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                     .detectDiskReads()
                     .detectDiskWrites()
                     .detectNetwork()  
                     .penaltyLog()
                     .build());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add some data from variables into my database using sqlite library
I want to make some inserts into a table, using some data from powerbuilder.
I want to insert some session data in a database. I have one table
I'm woondring how to insert data into my DataBase using Ajax in ASP.net MVC.
I am attempting to insert data from a dictionary into a database. I want
I want to save some data into sqlite database and the read it, there
I have some data enclosed in HTML tags. I want to insert this data
here is myproblem, i want to make a php function to insert some random
So I'm trying to import some sales data into my MySQL database. The data
I want to insert a hash in the db using Storable::nfreeze but the data

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.