Issue
code for making an HTTP request using HttpURLConnection
.
try {
ds = new DataSource(cont);
final URL url = new URL("http://sms.abc.com:1980/MOB/SendDoc.aspx");
con = (HttpURLConnection) url.openConnection();
con.setDoInput(true);
con.setDoInput(true);
con.setChunkedStreamingMode(0);
con.addRequestProperty("Content-Type","application/json; charset=utf-8");
con.setRequestMethod("POST");
try{
ds.open(); //Open Data connection
}catch(Exception ex){
Log.v("jarvis", ex.getMessage());
}finally {
ds.open();
}
List<EmpAttandance> userList = new LinkedList<>();
userList = ds.sendSMS();
EmpAttandance emp = new EmpAttandance();
JSONArray JA = new JSONArray(userList);
for (int i=0;i <JA.length();i++){
JSONObject JO = (JSONObject) JA.get(i);
emp = userList.get(i);
JO.put("DocID1",emp.getEMP_ID());//DocID1variable that are present in web Asp
JO.put("EMPID1",emp.getDOC_ID());
JO.put("DATE1",emp.getDATE());
JO.put("PHONENUM1",emp.getPHONE_NUM());
JO.put("LON1",emp.getLOC_LON());
JO.put("LAT1",emp.getLOC_LAT());
JO.put("SA1",emp.getSA());
JO.put("COID1",emp.getCO_ID());
OutputStream out = new BufferedOutputStream(con.getOutputStream());
out.write(JO.toString().getBytes());
out.flush();
out.close();
}
ds.close();
int result =con.getResponseCode();
if (result==200){
InputStream in = new BufferedInputStream(con.getInputStream());
reader = new BufferedReader(new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
String line= null;
while ((line = reader.readLine()) != null){
Status=line;
}
}
} catch (Exception e) {
e.printStackTrace();
}
SQLITE data Query:
public List<EmpAttandance> sendSMS() {
List<EmpAttandance> personLinkedList = new LinkedList<>();
SQLiteDatabase db = dbHelper.getWritableDatabase();
String query = "SELECT * FROM " + DatabaseHelper.TBL_DOCATTANDANCE;
Cursor cursor = db.rawQuery(query,null);
EmpAttandance emp;
if (cursor.moveToFirst()) {
do {
emp = new EmpAttandance();
emp.setDOC_ID(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_DI_USER_ID)));
emp.setPHONE_NUM(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_DA_PHONE_NUM)));
emp.setCO_ID(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_DA_CO_ID)));
emp.setSA(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_DA_SA)));
emp.setDATE(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_DA_DATE)));
emp.setEMP_ID(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_DA_EMP_ID)));
emp.setLOC_LAT(cursor.getDouble(cursor.getColumnIndex(DatabaseHelper.COL_DA_LOC_LAT)));
emp.setLOC_LON(cursor.getDouble(cursor.getColumnIndex(DatabaseHelper.COL_DA_LOC_LON)));
personLinkedList.add(emp);
} while (cursor.moveToNext());
}
return personLinkedList;
}
But I get the Error On json Array[0] is null here is the error
V/jarvis: Connection Checkcom.android.okhttp.internal.huc.HttpURLConnectionImpl:http://sms.iblhc.com:1980/MOB/SendDoc.aspx
V/jarvis: POSTcom.android.okhttp.internal.huc.HttpURLConnectionImpl:http://sms.iblhc.com:1980/MOB/SendDoc.aspx
V/jarvis: list[]
V/jarvis: userList Data array[com.example.zed.androidapplication.EmpAttandance@bcaffce, com.example.zed.androidapplication.EmpAttandance@6b3deef, com.example.zed.androidapplication.EmpAttandance@45cedfc, com.example.zed.androidapplication.EmpAttandance@8fc4c85, com.example.zed.androidapplication.EmpAttandance@69b4ada, com.example.zed.androidapplication.EmpAttandance@f897b0b, com.example.zed.androidapplication.EmpAttandance@688ede8]
W/System.err: org.json.JSONException: Value at 0 is null.
W/System.err: at org.json.JSONArray.get(JSONArray.java:289)
W/System.err: at com.example.zed.androidapplication.SendDocAttd.doInBackground(SendDocAttd.java:67)
W/System.err: at com.example.zed.androidapplication.SendDocAttd.doInBackground(SendDocAttd.java:22)
W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:305)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
W/System.err: at java.lang.Thread.run(Thread.java:761)
Can some one tell me where i did wrong. ?
Solution
I manege to upload my Sqlite Data to remote Server Using VolLey. And its work Perfectly Fine.
//region Sending
try{
ds = new DataSource(cont);
ds.open();
final Cursor cursor = ds.send();
if(cursor.moveToFirst())
do {
//region Sending Variables
final String CELL_NO = cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_DA_PHONE_NUM));
final String M_DATE = cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_DA_DATE));
final String M_TIME =cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_DA_TIME));
final String EMP_ID = cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_DA_EMP_ID));
final String UserID = String.valueOf(LoginActivity.ID) ;
final String DOC_ID = cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_DA_DOC_ID));
final String SA= cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_DA_SA));
final String NOTE = " - ";
final String CO_ID = cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_DA_CO_ID));
final String LAT =cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_DA_LOC_LAT));
final String LNG=cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_DA_LOC_LON));
//endregion
//region Sending Data
Log.v("jarvis","Line = 1");
StringRequest stringRequest = new StringRequest(Request.Method.POST,url,
new Response.Listener<String>(){
@Override
public void onResponse(String response) {
ds.updateStatus(DOC_ID,M_DATE,M_TIME,EMP_ID); //true
Toast.makeText(cont,"Uploaded And Saved.." ,Toast.LENGTH_SHORT).show();
Log.v("jarvis","Line = 2");
}
}
, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.v("jarvis","Line = 3");
ds.updateFalse(DOC_ID,M_DATE,M_TIME,EMP_ID); //true
Toast.makeText(cont,"Error.." ,Toast.LENGTH_SHORT).show();
error.printStackTrace();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Log.v("jarvis","Line = 4");
Map<String, String> param = new HashMap<String, String>();
param.put("CELL_NO", CELL_NO);
param.put("M_DATE", M_DATE);
param.put("M_TIME", M_TIME);
param.put("EMP_ID", EMP_ID);
param.put("DOC_ID", DOC_ID);
param.put("SA", SA);
param.put("NOTE", NOTE);
param.put("CO_ID", CO_ID );
param.put("LAT", LAT);
param.put("LNG",LNG);
param.put("USER_ID",UserID);
return param;
}
};
Log.v("jarvis","Line = 5");
MySingleton.getmInstance(cont).addToRequestQue(stringRequest);
//endregion
Log.v("jarvis","Line = 6");
}while (cursor.moveToNext());
}catch(Exception e){}
//endregion
Answered By - Muhammad Zeeshan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.