-2

I'm making an app which simply send data to a database with POST metod. The problem I am encountering is the app doesn't do nothing when it has to send: no crashes, no errors, nothing. I've tested the PHP code alone and it works, so I'm sure that the problem is in the code but I can't find it. Can you help me? I'm sorry for the var language which is not english.

Android code:

public class AggiungiProdotto extends Activity {

  private static String indirizzo = "http://10.0.2.2/tesina/Aggiungi_Ordinazione";


  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.aggiungi_prodotto);
    new AggiungoOrdinazione().execute();
  }

  private class AggiungoOrdinazione extends AsyncTask < String, Void, String > {

    @Override
    protected void onPreExecute() {
      super.onPreExecute();
    }

    @Override
    protected String doInBackground(String...arg0) {
      InputStream is = null;
      String result = "";
      JSONObject jsonResult = null;
      TextView tv;
      //Intent intent = getIntent();
      String Nome = new String();

      //String Tavolo = intent.getStringExtra("Tavolo");
      int NumTavolo = 1;
      String Tavolo = Integer.toString(NumTavolo);
      Nome = "ciao";

      HttpPost httppost = new HttpPost(indirizzo);

      HttpParams httpParams = new BasicHttpParams();

      int timeOutConnection = 5000;
      HttpConnectionParams.setConnectionTimeout(httpParams, timeOutConnection);
      int timeoutSocket = 5000;
      HttpConnectionParams.setSoTimeout(httpParams, timeoutSocket);
      HttpClient httpclient = new DefaultHttpClient(httpParams);

      List < NameValuePair > Comanda = new ArrayList < NameValuePair > ();
      Comanda.add(new BasicNameValuePair("Nome", Nome));
      Comanda.add(new BasicNameValuePair("Tavolo", Tavolo));


      try {
        httppost.setEntity(new UrlEncodedFormEntity(Comanda));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();

        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));

        StringBuilder sb = new StringBuilder();
        String line = null;
        while((line = reader.readLine()) != null) {
          sb.append(line + "\n");
        }
        is.close();
        result = sb.toString();
        jsonResult = new JSONObject(result);



      } catch(UnsupportedEncodingException e) {
        e.printStackTrace();
      } catch(ClientProtocolException e) {
        e.printStackTrace();
      } catch(IOException e) {
        e.printStackTrace();
      } catch(JSONException e) {
        e.printStackTrace();
      }

      return null;
    }
  }
  protected void onPostExecute() {
  }
}

PHP code:

$Nome;
$Tavolo;

$Risposta = array();

//Apro la connessione con il server mySQL

$Conn = mysql_connect("localhost", "root", "");
if(!$Conn) {
    die('Errore di connessione: '.mysql_error());
}


//Seleziono il mio database
$DBS = mysql_select_db('ordinazioni', $Conn);
if(!$DBS) {
    die('Accesso al database non riuscito: '.mysql_error());
}

//echo "Mi sono connesso!";

$Nome = $_POST['Nome'];
$Tavolo = $_POST['Tavolo'];

$strSQL = mysql_query("INSERT INTO Comande (Nome, Tavolo) VALUES ('$Nome', '$Tavolo')");

if($strSQL) {
    $Risposta["Esito"] = 1;
    $Risposta["Messaggio"] = "Comanda aggiunta!";
    echo json_encode($Risposta);
    echo "Ho aggiunto!";
}
else {
    $Risposta["Esito"] = 0;
    $Risposta["Messaggio"] = "Errore nell'aggiunta!";
    echo "Non ho aggiunto!";
    echo json_encode($Risposta);
}

?>

Manifest:

<uses-sdk
    android:minSdkVersion="9"
    android:targetSdkVersion="17" />
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.gabriele.tesina.Menu_Iniziale"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.gabriele.tesina.Listino"
        android:label="Il Nostro Menù" >
    </activity>
    <activity
        android:name="com.gabriele.tesina.Ordinazioni_Cuoco"
        android:label="Le pietanze da preparare" >
    </activity>
    <activity
        android:name="com.gabriele.tesina.Ordinazioni_Pizza"
        android:label="Le pizze da infornare" >
    </activity>
    <activity android:name="com.gabriele.tesina.Primi" />
    <activity android:name="com.gabriele.tesina.Secondi" />
    <activity android:name="com.gabriele.tesina.Contorni" />
    <activity android:name="com.gabriele.tesina.Dolci" />
    <activity android:name="com.gabriele.tesina.Pizze" />
    <activity android:name="com.gabriele.tesina.Bevande" />
    <activity
        android:name="com.gabriele.tesina.AggiungiProdotto"
        android:label="Aggiungi Prodotto" >
    </activity>
</application>

LogCat:

03-09 14:43:09.347: W/Trace(771): Unexpected value from nativeGetEnabledTags: 0
03-09 14:43:09.737: W/System.err(771): org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
03-09 14:43:09.737: W/System.err(771):  at org.json.JSON.typeMismatch(JSON.java:111)
03-09 14:43:09.737: W/System.err(771):  at org.json.JSONObject.<init>(JSONObject.java:158)
03-09 14:43:09.747: W/System.err(771):  at org.json.JSONObject.<init>(JSONObject.java:171)
03-09 14:43:09.747: W/System.err(771):  at com.gabriele.tesina.AggiungiProdotto$AggiungoOrdinazione.doInBackground(AggiungiProdotto.java:113)
03-09 14:43:09.758: W/System.err(771):  at com.gabriele.tesina.AggiungiProdotto$AggiungoOrdinazione.doInBackground(AggiungiProdotto.java:1)
03-09 14:43:09.758: W/System.err(771):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-09 14:43:09.767: W/System.err(771):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
03-09 14:43:09.767: W/System.err(771):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-09 14:43:09.777: W/System.err(771):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
03-09 14:43:09.777: W/System.err(771):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
03-09 14:43:09.777: W/System.err(771):  at java.lang.Thread.run(Thread.java:856)

Thank you for the helps.

hexacyanide
  • 88,222
  • 31
  • 159
  • 162
Eulante
  • 309
  • 2
  • 4
  • 15

2 Answers2

0

I don't see any internet permission in your manifest. You need to explicitly grant an app internet access if you want it to be able to connect.

Add this to your manifest:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

Source

Community
  • 1
  • 1
chrki
  • 6,143
  • 6
  • 35
  • 55
0

DOCTYPE of type java.lang.String cannot be converted to JSONObject

You need to check the request format it personally and Why your server its not sending the Valid JSON.

Debug your application to check the response from server.

To check the JSON is valid or not.

Use JSON Lint.

Ajay S
  • 48,003
  • 27
  • 91
  • 111