Reputation: 1
I have been developing an app and fair prediction is a part of it.. it contains a webpart too.. but i cant login into that part.. the app is terminating by that time.. the login page is linked to a jsp page and inturn to a mysql database.. i m posting the code here.. pls help me.. thanks in advance..
package com.example.tracemeendpointsjson;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class TLogin extends Activity {
EditText uname,pwd;
Button log,signup;
String response="";
NameValuePair v;
String LoginID,LoginPassword;
HttpRequestClass mynetworkclass;
ArrayList<NameValuePair> namevaluepair;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tlogin);
uname=(EditText) findViewById(R.id.uname);
pwd=(EditText) findViewById(R.id.pwd);
log=(Button) findViewById(R.id.login);
signup=(Button) findViewById(R.id.bSignup);
log.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
LoginID=uname.getText().toString();
LoginPassword=pwd.getText().toString();
//CommonClassForLogin q=(CommonClassForLogin) getApplication();
//q.myUID=LoginID;
String url="http://"+details.ip+"taxinew/mobile/login.jsp";
new AsyncTaskLogin().execute(url,LoginID,LoginPassword);
}
});
}
public class AsyncTaskLogin extends AsyncTask<String, Integer, String>
{
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
mynetworkclass=new HttpRequestClass();
namevaluepair=new ArrayList<NameValuePair>();
namevaluepair.add(new BasicNameValuePair("username",params[1]));
namevaluepair.add(new BasicNameValuePair("password",params[2]));
try {
String result=mynetworkclass.getinformation(params[0],namevaluepair);
Log.d("dsfwewerte", result);
result=result.replaceAll("\\r|\\n", "");
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
//Log.d("222222222", result);
if(result.equals("0"))
{
Toast.makeText(getApplicationContext(),"Welcomes You", Toast.LENGTH_SHORT).show();
Intent in=new Intent(getApplicationContext(), Trip.class);
startActivity(in);
//Log.d("11111111111", result);
}
else
{
Toast.makeText(getApplicationContext(),"Wrong Entry", Toast.LENGTH_SHORT).show();
Intent in1=new Intent(getApplicationContext(), Home.class);
startActivity(in1);
//Log.d("0000000000", result);
}
}
}
}
package com.example.tracemeendpointsjson;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
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.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
import android.widget.Toast;
public class HttpRequestClass {
HttpClient httpclient;
public String getinformation(String url,ArrayList<NameValuePair> namevaluepair) throws ParseException, IOException
{
httpclient=new DefaultHttpClient();
HttpPost request=new HttpPost(url);
request.setEntity(new UrlEncodedFormEntity(namevaluepair));
try
{
HttpResponse respnse=httpclient.execute(request);
int status=respnse.getStatusLine().getStatusCode();
if(status==200)
{
HttpEntity e=respnse.getEntity();
String data=EntityUtils.toString(e);
return data;
}
}
catch(ClientProtocolException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
return null;
}
public String getsingleinformation(String url) throws ParseException, IOException
{
httpclient=new DefaultHttpClient();
HttpPost request=new HttpPost(url);
try
{
HttpResponse respnse=httpclient.execute(request);
int status=respnse.getStatusLine().getStatusCode();
if(status==200)
{
HttpEntity e=respnse.getEntity();
String data=EntityUtils.toString(e);
return data;
}
}
catch(ClientProtocolException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
return null;
}
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tracemeendpointsjson"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:icon="@drawable/traffic"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library android:name="com.google.android.maps"/>
<activity
android:name="com.example.tracemeendpointsjson.Home"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"></activity>
<activity
android:name=".UploadDetails"></activity>
<activity
android:name=".CurrentLocation"></activity>
<activity
android:name=".GmailLogin"></activity>
<activity
android:name=".MailReceivers"></activity>
<activity
android:name=".ShowWeather"></activity>
<activity
android:name=".DisplayWeather"></activity>
<activity
android:name=".Trip"></activity>
<activity
android:name=".ShowRoute"></activity>
<activity
android:name=".Parking"></activity>
<activity
android:name=".Estfare"></activity>
<activity
android:name=".GetFare"></activity>
<activity
android:name=".TLogin">
</activity>
<activity
android:name=".ParkingPlaceShow"></activity>
<service android:name=".ServiceClass" >
</service>
<!-- <service android:name=".GPSTracker"></service> -->
</application>
</manifest>
03-20 09:51:30.866: E/AndroidRuntime(476): FATAL EXCEPTION: main
03-20 09:51:30.866: E/AndroidRuntime(476): java.lang.NullPointerException
03-20 09:51:30.866: E/AndroidRuntime(476): at com.example.tracemeendpointsjson.TLogin$AsyncTaskLogin.onPostExecute(TLogin.java:85)
03-20 09:51:30.866: E/AndroidRuntime(476): at com.example.tracemeendpointsjson.TLogin$AsyncTaskLogin.onPostExecute(TLogin.java:1)
03-20 09:51:30.866: E/AndroidRuntime(476): at android.os.AsyncTask.finish(AsyncTask.java:417)
03-20 09:51:30.866: E/AndroidRuntime(476): at android.os.AsyncTask.access$300(AsyncTask.java:127)
03-20 09:51:30.866: E/AndroidRuntime(476): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
03-20 09:51:30.866: E/AndroidRuntime(476): at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 09:51:30.866: E/AndroidRuntime(476): at android.os.Looper.loop(Looper.java:130)
03-20 09:51:30.866: E/AndroidRuntime(476): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-20 09:51:30.866: E/AndroidRuntime(476): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 09:51:30.866: E/AndroidRuntime(476): at java.lang.reflect.Method.invoke(Method.java:507)
03-20 09:51:30.866: E/AndroidRuntime(476): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-20 09:51:30.866: E/AndroidRuntime(476): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-20 09:51:30.866: E/AndroidRuntime(476): at dalvik.system.NativeStart.main(Native Method)
03-20 09:51:30.896: W/ActivityManager(83): Force finishing activity com.example.tracemeendpointsjson/.TLogin
03-20 09:51:31.428: W/ActivityManager(83): Activity pause timeout for HistoryRecord{40803618 com.example.tracemeendpointsjson/.TLogin}
03-20 09:51:33.555: E/InputDispatcher(83): channel '40800e70 com.example.tracemeendpointsjson/com.example.tracemeendpointsjson.Home (server)' ~ Consumer closed input channel or an error occurred. events=0x8
03-20
09:51:33.555: E/InputDispatcher(83): channel '40800e70 com.example.tracemeendpointsjson/com.example.tracemeendpointsjson.Home (server)' ~ Channel is unrecoverably broken and will be disposed!
Login.jsp is as follows:
<%--
Document : login
--%>
<%@page import="javax.swing.JOptionPane,DB.DbCon"%>
<%@page import="org.json.JSONObject" %>
<%@page import="org.json.JSONArray" %>
<%@page import="java.sql.ResultSet"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
String username=request.getParameter("username");
String password=request.getParameter("password");
JSONObject jo= new JSONObject();
//JSONArray ja=new JSONArray();
String query="select * from Userdetails where name='"+username+"' and pwd='"+password+"' ";
DbCon o=new DbCon();
ResultSet rs=o.selects(query);
if(rs.next()){
jo.accumulate("0", true);
}
else
{
jo.accumulate("1", false);
}
out.print(jo);
%>
Upvotes: 0
Views: 102
Reputation: 3485
You can Return result
"with No data"
with default
and match it, you can also debug and check your Response what you get
Upvotes: 0
Reputation: 23638
Just try to return the result
in your doInBackground
method at the end besides null. At the end of that method you are returning null that is why its throwing the nullpointer excepection
.
Or you need to check for the null value of the result in your onPostExecute
method to prevent the exception to be thrown.
@Override protected String doInBackground(String... params) { // TODO Auto-generated method stub mynetworkclass=new HttpRequestClass(); namevaluepair=new ArrayList<NameValuePair>(); namevaluepair.add(new BasicNameValuePair("username",params[1])); namevaluepair.add(new BasicNameValuePair("password",params[2])); try { String result=mynetworkclass.getinformation(params[0],namevaluepair); Log.d("dsfwewerte", result); result=result.replaceAll("\\r|\\n", ""); return result; } catch (Exception e) { e.printStackTrace(); } return result; <----- Check this line. } @Override protected void onPostExecute(String result) { // TODO Auto-generated method stub super.onPostExecute(result); //Check for the result is null or not. if(result != null) { if(result.equals("0")) {} } }
Upvotes: 0
Reputation: 501
Try these code.. I think these is the problem.
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String result;
mynetworkclass=new HttpRequestClass();
namevaluepair=new ArrayList<NameValuePair>();
namevaluepair.add(new BasicNameValuePair("username",params[1]));
namevaluepair.add(new BasicNameValuePair("password",params[2]));
try {
result=mynetworkclass.getinformation(params[0],namevaluepair);
Log.d("dsfwewerte", result);
result=result.replaceAll("\\r|\\n", "");
return result;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
Upvotes: 2
Reputation: 18430
Your result
is null, a simple change to your if-condition could solve this problem
if("0".equals(result))
Upvotes: 1