0

I am trying to connect to a PHP page in my Android app. I have the permissions set. However, I keep getting Invalid ip address. It seems I need AsyncTask. However, I don't know how to set it up, maybe a class that runs separately like this: How to fix android.os.NetworkOnMainThreadException? , but on that link I have no idea why they are returning the RSSFeed

The actual website link works. It is just my app throwing this error.

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

nameValuePairs.add(new BasicNameValuePair("id", "123"));
nameValuePairs.add(new BasicNameValuePair("name", "test name"));
nameValuePairs.add(new BasicNameValuePair("family_memb_id", "456");

try
{
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://mysite/insert.php");

    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    is = entity.getContent();
 }

 catch(Exception e)
 {
     Log.e("app", "Failed 1: " + e.toString());
 }

 try
 {
     BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
     StringBuilder sb = new StringBuilder();

     while ((line = reader.readLine()) != null)
         sb.append(line + "\n");

     is.close();
     result = sb.toString();
 }

 catch(Exception e)
 {
     Log.e("app", "Failed 2: " + e.toString());
 }

 try
 {
     JSONObject json_data = new JSONObject(result);
     code=(json_data.getInt("code"));

     if(code==1)
     {
         Toast.makeText(getBaseContext(), "Inserted Successfully",
                        Toast.LENGTH_SHORT).show();
     }
     else
     {
         Toast.makeText(getBaseContext(), "Sorry, Try Again",
                        Toast.LENGTH_LONG).show();
     }
 }
 catch(Exception e)
 {
     Log.e("app", "Failed 3: " + e.toString());
 }

Error Log:

Failed 1: android.os.NetworkOnMainThreadException
Failed 2: java.lang.NullPointerException: lock == null
Failed 3: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference

This is not a duplicate of null pointer exception; although my log shows a null pointer exception, my problem happens before that. The null pointer is a side effect of what the real issue is.

Community
  • 1
  • 1
J_Strauton
  • 2,270
  • 3
  • 28
  • 70
  • @JeffBridgman although my log shows a null pointer exception, my problem happens before that. The null pointer is a side effect of what the real issue is. – J_Strauton May 15 '15 at 01:58

1 Answers1

0

Yeah, you're on the right track with thinking you need an AsyncTask!

You have to run all network communications in a thread separate to the main UI. It's done this way because putting network requests in the UI thread would likely result in a poor user experience; basically your app would hang while waiting for responses from the servers you're contacting.

So getting a separate AsyncTask to handle your network communications is the way to go.

Edit: Here's an indepth tutorial on how to get you started with network comms in an AsyncTask.

Another Edit: Your network code is going to go into the AsyncTask. Assuming you're passing in your NameValuePairs your code might look something like this.

/* somewhere in your UI thread */
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

nameValuePairs.add(new BasicNameValuePair("id", "123"));
nameValuePairs.add(new BasicNameValuePair("name", "test name"));
nameValuePairs.add(new BasicNameValuePair("family_memb_id", "456");
AsyncTask httpAsyncTask = new HttpAsyncTask();
httpAsyncTask.execute(nameValuePairs);


/* your AsyncTask */

public class AsyncTask HttpAsyncTask extends AsyncTask<ArrayList<NameValuePair>, Void, Boolean>(){
    @Override
    protected Boolean doInBackground(ArrayList<NameValuePair>... params)
    {
         ArrayList<NameValuePair> nameValuePairs = params[0];
         /* Your HTTP Code Here */
    }

}

It's worth point out that using HttpURLConnection is recommended instead of DefaultHttpClient. Its API is easier to use, it's still actively being improved and it's more efficient, which in turn improves speed and saves battery.

If you decide to make the switch to HttpURLConnection then the code in this SO answer shows how you'd go about posting your namevaluepairs.

Community
  • 1
  • 1
skyjacks
  • 1,154
  • 9
  • 7