Hello developers,
I want call web service on if particular word is found in message.but it
shows null pointer exception.
//sms Receiver code
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class SmsReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] =
SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from "
+msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
if(str.contains("abc"))
{
Toast.makeText(context, "found",
Toast.LENGTH_LONG).show();
JsonParsingActivity jpa=new JsonParsingActivity();
jpa.check();
// Toast.makeText(context, "web service
acccess",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(context, "Not found",
Toast.LENGTH_LONG).show();
}
}
//---display the new SMS message---
//Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
}
//JsonParsingActivity.
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.google.gson.Gson;
public class JsonParsingActivity extends Activity {
String url =
"http://search.twitter.com/search.json?q=javacodegeeks";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void check()
{
InputStream source = retrieveStream(url);
Gson gson = new Gson();
Reader reader = new InputStreamReader(source);
SearchResponse response = gson.fromJson(reader,
SearchResponse.class);
Toast.makeText(this, response.query,
Toast.LENGTH_SHORT).show();
List<Result> results = response.results;
for (Result result : results) {
Toast.makeText(this, result.fromUser,
Toast.LENGTH_SHORT).show();
}
}
private InputStream retrieveStream(String url) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(url);
try {
HttpResponse getResponse = client.execute(getRequest);
final int statusCode =
getResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w(getClass().getSimpleName(), "Error " + statusCode + "
for URL " +url);
return null;
}
HttpEntity getResponseEntity = getResponse.getEntity();
return getResponseEntity.getContent();
}
catch (IOException e) {
getRequest.abort();
Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
}
return null;
}
//Result.java
import com.google.gson.annotations.SerializedName;
public class Result {
@SerializedName("from_user_id_str")
public String fromUserIdStr;
@SerializedName("profile_image_url")
public String profileImageUrl;
@SerializedName("created_at")
public String createdAt;
@SerializedName("from_user")
public String fromUser;
@SerializedName("id_str")
public String idStr;
public Metadata metadata;
@SerializedName("to_user_id")
public String toUserId;
public String text;
public long id;
@SerializedName("from_user_id")
public String from_user_id;
@SerializedName("iso_language_code")
public String isoLanguageCode;
@SerializedName("to_user_id_str")
public String toUserIdStr;
public String source;
}
//
import java.util.List;
import com.google.gson.annotations.SerializedName;
public class SearchResponse {
public List<Result> results;
@SerializedName("max_id")
public long maxId;
@SerializedName("since_id")
public int sinceId;
@SerializedName("refresh_url")
public String refreshUrl;
@SerializedName("next_page")
public String nextPage;
@SerializedName("results_per_page")
public int resultsPerPage;
public int page;
@SerializedName("completed_in")
public double completedIn;
@SerializedName("since_id_str")
public String sinceIdStr;
@SerializedName("max_id_str")
public String maxIdStr;
public String query;
}
//metedata.java
import com.google.gson.annotations.SerializedName;
public class Metadata {
@SerializedName("result_type")
public String resultType;
}
//AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mobisys.android.sms_messaging"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission
android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/icoandroid:label="@string/app_name">
<activity android:name=".SMSActivity"
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=".JsonParsingActivity">
</activity>
<receiver android:name=".SmsReceiver">
<intent-filter>
<action android:name=
"android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
* //Whernerver any perticualer word is found like abc it calls
web servcice but it shows
NULLPOINTEREXCEPTION here.
SearchResponse response = gson.fromJson(reader,
SearchResponse.class);
Toast.makeText(this, response.query,
Toast.LENGTH_SHORT).show();
List<Result> results = response.results;*
plz any body Help me..............
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en