-1

I am programming an Android app which works with a MySQL database .This database has a table in it to record some results.

My app is able to communicate with that table and do CRUD operations(create, delete, read and update) using PHP.

However what I want now is to create new tables in that DB programmatically. I have used and tested the following PHP code manually and it works fine but it gives an error when making the request from my app.

Following are code snippets and error message. Answers or discussions are very appreciated and welcome.

Here is where I send create table request:

table_name = "Background_Apps_Test";
            params.add(new BasicNameValuePair("table_name", table_name));
            json = jsonParser.makeHttpRequest(Config.FILE_UPLOAD_URL+"create_table.php", "POST", params);

Here is my php file that I request creating through:

<?php

if (isset($_POST['table_name']){

$table_name = $_POST['table_name'];

require_once __DIR__ . '/db_connect.php';

$db = new DB_CONNECT();

$query = "
CREATE TABLE $table_name (
    pid int(11) primary key auto_increment,
    l_type varchar(100) not null,
    l_info varchar(5000) not null,
    l_ip varchar(15),
    created_at timestamp default now()
)";
}
?>

This is the error message:

E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.os.NetworkOnMainThreadException
        at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1128)
        at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
        at libcore.io.IoBridge.connectErrno(IoBridge.java:159)
        at libcore.io.IoBridge.connect(IoBridge.java:112)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
        at java.net.Socket.connect(Socket.java:857)
        at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
        at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
        at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
        at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
        at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:365)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
        at mtb.saeed.com.mytestingbenchmark.JSONParser.makeHttpRequest(JSONParser.java:52)
        at mtb.saeed.com.mytestingbenchmark.XmlFileCreator.Automate(XmlFileCreator.java:400)
        at mtb.saeed.com.mytestingbenchmark.XmlFileCreator$12.onClick(XmlFileCreator.java:260)
        at android.view.View.performClick(View.java:4211)
        at android.view.View$PerformClick.run(View.java:17446)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:153)
        at android.app.ActivityThread.main(ActivityThread.java:5299)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
        at dalvik.system.NativeStart.main(Native Method)
Abhishek Ghosh
  • 2,593
  • 3
  • 27
  • 60
Saeed
  • 25
  • 1
  • 8
  • Are you using the same DB user for your app and the PHP code ? same domain? same level of database permissions ? – Maximus2012 Mar 31 '15 at 17:07
  • 2
    1. Sweet jesus don't just cram unvalidated POST data into a SQL query! 2. Creating multiple identical tables is generally poor practice. One table schema == one table. – Sammitch Mar 31 '15 at 17:09
  • How would you describe it so while you don't know what is the usage for multiple tables? I am running a benchmarking system that test many other apps. So I need to make a single table for each tested app and that will help producing well structured results for use. Thanks for commenting anyway bro... – Saeed Mar 31 '15 at 21:04

1 Answers1

0

The problem is with your Android code. In fact, Android imposes that you run all Networking code in a different thread than the main thread. Try to move your code (the one calling the server) to an AsyncTask for instance.

Edited : Here is your exact problem : How to fix android.os.NetworkOnMainThreadException?

Community
  • 1
  • 1
Souhaib Guitouni
  • 860
  • 1
  • 7
  • 24