Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. .

MainActivity.java
Java
package com.javapapers.android.form;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import com.javapapers.android.form.R;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class FrmActivity extends Activity {

	Button mButton;
	EditText mEdit;
	TextView mText;
	EditText mDate;

	String id;
	InputStream is;
	String line;
    String result;
    
    
    
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frm);
        mButton = (Button)findViewById(R.id.button1);
        
        mButton.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View view) {
        		mEdit   = (EditText)findViewById(R.id.editText1);
        		mText = (TextView)findViewById(R.id.textView1);
        		id = ((EditText) findViewById(R.id.editText2)).getText().toString();
        		System.out.println(id);
        		select();
        	}
        });
    }
    
    
    public void select()
    {
    	ArrayList<namevaluepair> nameValuePairs = new ArrayList<namevaluepair>();
 
	
	nameValuePairs.add(new BasicNameValuePair("id",id));
    	
    	
		try
    	{
		HttpClient httpclient = new DefaultHttpClient();
	        HttpPost httppost = new HttpPost("http://192.168.1.3/select.php");
	        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
	        HttpResponse response = httpclient.execute(httppost); 
	        HttpEntity entity = response.getEntity();
	        is = entity.getContent();
	        Log.e("pass 1", "connection success ");
	}
        catch(Exception e)
	{
        	Log.e("Fail 1", e.toString());
	    	Toast.makeText(getApplicationContext(), "Invalid IP Address",
			Toast.LENGTH_LONG).show();
	}     
        
  
		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();
	        Log.e("pass 2", "connection success ");
	}
        catch(Exception e)
    	{
		Log.e("Fail 2", e.toString());
	}     
       
   	try
    	{
        	JSONObject json_data = new JSONObject(result);
        	String name = (json_data.getString("name"));
		Toast.makeText(getBaseContext(), "Name : "+name,
			Toast.LENGTH_SHORT).show();
    	}
        catch(Exception e)
    	{
        	Log.e("Fail 3", e.toString());
    	}
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_frm, menu);
        return true;
    }    
}



activity_main.xml
XML
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 
    <edittext
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="39dp"
        android:padding="11dp"
        android:hint="Id"
        android:ems="10"
        android:inputType="number" >
    </edittext>
    <requestfocus />
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:padding="11dp"
        android:text="Select" />
 
</relativelayout>


this my code. . and its shows the following error
09-06 16:56:09.414: E/Fail 1(7064): android.os.NetworkOnMainThreadException
09-06 16:56:09.424: E/Fail 2(7064): java.lang.NullPointerException
09-06 16:56:09.434: E/Fail 3(7064): java.lang.NullPointerException

pls anyone resolve this. .
Posted
Updated 6-Sep-13 1:49am
v2

1 solution

Don't do long running operations on your Main thread. Network operations are considered long running and the framework will throw (like you've just experienced) if you try this.

Create an AsyncTask and delegate the network code to that instead.

AsyncTask API documentation[^]

Hope this helps,
Fredrik
 
Share this answer
 
Comments
monishamurugesan 6-Sep-13 7:57am    
Thank U so much. . but i don't know how to use this(Async Task).
Can u Help me. .pls
Menon Santosh 7-Sep-13 4:57am    
i agree with u, my + 5
Ganesh KP 7-Sep-13 7:05am    
Use AsyncTask to do all network related operations for Android version 2.3 and above, other wise your app will crash. see this link for how to use AsyncTask

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900