I want to send Android's inbox messages to the web C# service, but I am getting this error attempt to invoke virtual method ' int java.lang.String.length()' on a null object reference
. What could be the reason? Kindly help me resolve this. Have a good day.
Below is the code I am using:
private final String NAMESPACE3 = "http://tosman/";
private final String METHOD_NAME3 = "gelenkutusu";
private final String SOAP_ACTION3 = "http://tosman/gelenkutusu";
private final String URL3 = "http://takiprogrami-001-site1.smarterasp.net/Service1.asmx";
Uri uriSMSURI = Uri.parse("content://sms/inbox");
cur = getContentResolver().query(uriSMSURI, null, null, null,null);
String sms = "";
long tim;
if(cur.moveToFirst()) {
for(int i=0; i < cur.getCount(); i++) {
String date = cur.getString(cur.getColumnIndex(Sms.DATE));
Long timestamp = Long.parseLong(date);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy/HH:mm");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timestamp);
String finaldate = dateFormat.format(calendar.getTime());
smsDate = finaldate.toString();
InboxSmsBody=(cur.getString(cur.getColumnIndexOrThrow(Sms.BODY)).toString());
InboxSmsFrom=(cur.getString(cur.getColumnIndexOrThrow(Sms.ADDRESS)).toString());
InboxSmsDate=smsDate;
Log.e("BODY", InboxSmsBody);
new ToastMessageTask().execute(InboxSmsFrom, InboxSmsBody,smsDate);
// new ToastMessageTask().execute("");
cur.moveToNext();
}
}
cur.close();
private void inbox(String from, String body,String date) {
SoapObject request = new SoapObject(NAMESPACE3, METHOD_NAME3);
PropertyInfo DateInfo = new PropertyInfo();
DateInfo.setName("from");
DateInfo.setValue(from);
PropertyInfo BodyInfo = new PropertyInfo();
BodyInfo.setName("body");
BodyInfo.setValue(body);
PropertyInfo FromInfo = new PropertyInfo();
BodyInfo.setName("date");
BodyInfo.setValue(date);
request.addProperty(FromInfo);
request.addProperty(BodyInfo);
request.addProperty(DateInfo);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransportSE = new HttpTransportSE(URL3);
try {
httpTransportSE.call(SOAP_ACTION3, envelope);
SoapPrimitive soapPrimitive = (SoapPrimitive) envelope
.getResponse();
inboxsmsResult = soapPrimitive.toString();
Log.e("inboxsmsResult=", inboxsmsResult);
} catch (Exception e) {
Log.e("inboxsmsResult", e.getMessage());
}
}
private class ToastMessageTask extends AsyncTask<String, String, String> {
public String inboxsmsdate;
public String inboxsmsfrom;
public String inboxsmsbody;
public ToastMessageTask(){
this.inboxsmsdate=InboxSmsDate;
this.inboxsmsbody=InboxSmsBody;
this.inboxsmsfrom=InboxSmsFrom;
}
@Override
protected String doInBackground(String... params) {
inbox(inboxsmsdate, inboxsmsbody,inboxsmsfrom);
return null;
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
}
This is a my C# Web service:
[WebMethod]
public string inbox(string from, string body, string date)
{
string connString = System.Configuration.ConfigurationManager.ConnectionStrings["DB_9C047C_deneme2012"].ConnectionString;
using (SqlConnection connection = new SqlConnection(connString))
{
connection.Open();
using (SqlCommand command = new SqlCommand())
{
command.Connection = connection;
command.CommandText = "INSERT INTO Table_1([FROM],BODY,DATE)VALUES(@FROM,@BODY,@DATE)";
command.Parameters.AddWithValue("@FROM", from);
command.Parameters.AddWithValue("@BODY", body);
command.Parameters.AddWithValue("@DATE", date);
// SqlCeDataReader reader = command.ExecuteReader();
command.ExecuteNonQuery();
return "true";
}
}
}