title: Connect android with php mySQL
body: <p>i am kind a new in android programming. I am trying to create an app that will communicate with a database on the internet. As a help i am using an project from <a href="http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/" rel="nofollow">androidhive</a>. 
  For begining i want to create app that will send some data to the db on localhost. I am using WAMP. </p>
  
  <p>After running my app, and inserting data that i want to store on db, i'm getting bunch of exceptions and my application crashes.
  For example:</p>
  
  <ol>
  <li>E/Buffer Error(545): Error converting result java.lang.NullPointerException</li>
  <li>E/JSON Parser(545): Error parsing data org.json.JSONException: End of input at character 0 of </li>
  <li>E/AndroidRuntime(545): FATAL EXCEPTION: AsyncTask #1</li>
  <li>E/AndroidRuntime(545): java.lang.RuntimeException: An error occured while executing doInBackground()</li>
  </ol>
  
  <p>And a lot of others exceptions...</p>
  
  <p>My php scripts (or whatever you want to call them) are working fine, but I guess that problem is somewhere in JSONParser or other android activity... I've been searching for the solution but i couldn't find it, or i couldn't understand it. Also, i have downloaded code from androidhive, and it didn't work on my computer. Similar problem as with my project</p>
  
  <p>Activity for creating order (product or whatever):</p>
  
  <pre><code>package com.example.heywaiter;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import org.apache.http.NameValuePair;
  import org.apache.http.message.BasicNameValuePair;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import android.app.Activity;
  import android.app.ProgressDialog;
  import android.content.Intent;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  
  public class NewProductActivity extends Activity{
  
      private ProgressDialog pDialog;
  
  JSONParser jsonParser = new JSONParser();
  EditText inputTablle;
  EditText inputOrder;
  EditText inputQuantity;
  Button btnOrder;
  
  private static String url_create_product = "http://127.0.0.1/android_connect/create_product.php";
  private static final String TAG_SUCCESS = "success";
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.new_product);
  
      inputTablle = (EditText) findViewById(R.id.inputTablle);
      inputOrder = (EditText) findViewById(R.id.inputOrder);
      inputQuantity = (EditText) findViewById(R.id.inputQuantity);
      btnOrder = (Button) findViewById(R.id.btnOrder);
  
      btnOrder.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
              new CreateNewProduct().execute();
          }
      });
  }
  </code></pre>
  
  <p>Inner class:</p>
  
  <pre><code>class CreateNewProduct extends AsyncTask&lt;String, String, String&gt;{
  
  
  
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
          pDialog = new ProgressDialog(NewProductActivity.this);
          pDialog.setMessage("Sending order...");
          pDialog.setIndeterminate(false);
          pDialog.setCancelable(true);
          pDialog.show();
      }
  
      @Override
      protected String doInBackground(String... args) {
          String tablle = inputTablle.getText().toString();
          String drink = inputOrder.getText().toString();
          String quantity = inputQuantity.getText().toString();
  
          List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;();
          params.add(new BasicNameValuePair("tablle", tablle));
          params.add(new BasicNameValuePair("drink", drink));
          params.add(new BasicNameValuePair("quantity", quantity));
  
          JSONObject json = jsonParser.makeHttpRequest(url_create_product, "POST", params);
  
          Log.d("Create Response", json.toString());
  
          try{
  
              int success = json.getInt(TAG_SUCCESS);
  
              if(success ==1){
                  Intent i = new Intent(getApplicationContext(), MainActivity.class);
                  startActivity(i);
  
                  finish();
              }else{
  
              }
          }catch(JSONException e){
              e.printStackTrace();
          }
          return null;
      }
  
      @Override
      protected void onPostExecute(String result) {
          super.onPostExecute(result);
          pDialog.dismiss();
      }
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>JSONParser</p>
  
  <pre><code>package com.example.heywaiter;
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.UnsupportedEncodingException;
  import java.util.List;
  
  import org.apache.http.HttpEntity;
  import org.apache.http.HttpResponse;
  
  import org.apache.http.NameValuePair;
  import org.apache.http.client.ClientProtocolException;
  import org.apache.http.client.entity.UrlEncodedFormEntity;
  import org.apache.http.client.methods.HttpGet;
  import org.apache.http.client.methods.HttpPost;
  import org.apache.http.client.utils.URLEncodedUtils;
  import org.apache.http.impl.client.DefaultHttpClient;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import android.util.Log;
  
  public class JSONParser {
  
  
  static InputStream is = null;
  static JSONObject jObj = null;
  static String json = "";
  
  // constructor
  public JSONParser() {
  
  }
  
  // function get json from url
  // by making HTTP POST or GET mehtod
  public JSONObject makeHttpRequest(String url, String method,
          List&lt;NameValuePair&gt; params) {
  
      // Making HTTP request
      try {
  
          // check for request method
          if(method == "POST"){
              // request method is POST
              // defaultHttpClient
              DefaultHttpClient httpClient = new DefaultHttpClient();
              HttpPost httpPost = new HttpPost(url);
              httpPost.setEntity(new UrlEncodedFormEntity(params));
  
              HttpResponse httpResponse = httpClient.execute(httpPost);
              HttpEntity httpEntity = httpResponse.getEntity();
              is = httpEntity.getContent();
  
          }else if(method == "GET"){
              // request method is GET
              DefaultHttpClient httpClient = new DefaultHttpClient();
              String paramString = URLEncodedUtils.format(params, "utf-8");
              url += "?" + paramString;
              HttpGet httpGet = new HttpGet(url);
  
              HttpResponse httpResponse = httpClient.execute(httpGet);
              HttpEntity httpEntity = httpResponse.getEntity();
              is = httpEntity.getContent();
          }          
  
  
          } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
      } catch (ClientProtocolException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
  
      try {
          BufferedReader reader = new BufferedReader(new InputStreamReader(
                  is, "iso-8859-1"), 8);
          StringBuilder sb = new StringBuilder();
          String line = null;
          while ((line = reader.readLine()) != null) {
              sb.append(line + "\n");
          }
          is.close();
          json = sb.toString();
      } catch (Exception e) {
          Log.e("Buffer Error", "Error converting result " + e.toString());
      }
  
      // try parse the string to a JSON object
      try {
          jObj = new JSONObject(json);
      } catch (JSONException e) {
          Log.e("JSON Parser", "Error parsing data " + e.toString());
      }
  
      // return JSON String
      return jObj;
  
   }
  }
  </code></pre>
  
  <p>Also if you need my androidManifest:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.heywaiter"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="8"
          android:targetSdkVersion="14" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name="com.example.heywaiter.Splash"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;activity
              android:name="com.example.heywaiter.MainActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="com.example.heywaiter.MainActivity" /&gt;
  
                  &lt;category android:name="android.intent.category.DEFAULT" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;activity 
              android:name=".NewProductActivity"
              android:label="New Product"&gt;
          &lt;/activity&gt;
          &lt;activity
              android:name=".AllProductsActivity"
              android:label="All Products" &gt;
          &lt;/activity&gt;
      &lt;/application&gt;
      &lt;!--  Internet Permissions --&gt;
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /&gt;
  &lt;/manifest&gt;
  </code></pre>
  
  <p>Thanks in advance! :)</p>