如何在使用json数组时发送截击请求?我正在处理以下代码。
显示此错误
NetworkDispatcher.run:未处理的异常java.lang.RuntimeException:错误的URL 1 java.lang.RuntimeException:错误的URL 1
public class MyService2 extends Service {
RequestQueue mRequestQueue = Volley.newRequestQueue(getApplicationContext());
private ArrayList<HashMap<String, String>> contactList;
private final Context mContext= this;
final class MyThreadClass implements Runnable{
int service_id;
MyThreadClass(int service_id){
this.service_id = service_id;
}
@Override
public void run() {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query( ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
final JSONArray contacts = new JSONArray();
if (cur.getCount() > 0) {
contactList = new ArrayList<>();
while (cur.moveToNext()) {
JSONObject contact1 = new JSONObject();
String ID = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String NAME = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
System.out.println("name : " + NAME + ", ID : " + ID);
// get the phone number
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{ID}, null);
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put( Variables.TOKEN, new DefaultPreferences(MyService2.this)
.getValueString(Variables.TOKEN));
try {
contact1.put("Name", NAME);
contact1.put("ID", ID);
} catch (JSONException e) {
e.printStackTrace();
}
JSONArray jsonArray = new JSONArray();
while (pCur.moveToNext()) {
JSONObject _contact = new JSONObject();
String PHONE = pCur.getString(
pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println("phone" + PHONE);
try {
_contact.put("PHONE", PHONE);
jsonArray.put(_contact);
} catch (JSONException e) {
e.printStackTrace();
}
}
pCur.close();
// get email and type
Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{ID}, null);
JSONArray jsonEmailArray = new JSONArray();
while (emailCur.moveToNext()) {
JSONObject _contact = new JSONObject();
// This would allow you get several email addresses
// if the email addresses were stored in an array
String EMAIL = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String emailType = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
System.out.println("Email " + EMAIL + " Email Type : " + emailType);
}
emailCur.close();
try {
contact1.put("PHONES", jsonArray);
contact1.put("EMAILS", jsonEmailArray);
contacts.put(contact1);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
String url = "http://nojoto.com/appapi/userDetails.php";
VolleyMultipartRequest multipartRequest = new VolleyMultipartRequest( Request.Method.POST, url, new Response.Listener<NetworkResponse>() {
@Override
public void onResponse(NetworkResponse response) {
JSONObject object = new JSONObject();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
})
{
@Override
protected Map<String, String> getParams()throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("name", "NAME");
params.put("id", "ID");
params.put("phone", "PHONE");
params.put("email", "EMAIL");
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(mContext);
requestQueue.add(multipartRequest);
}
}
public MyService2() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Thread thread = new Thread(new MyThreadClass(startId));
thread.start();
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onTaskRemoved(Intent rootIntent) {
Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
restartServiceIntent.setPackage(getPackageName());
startService(restartServiceIntent);
super.onTaskRemoved(rootIntent);
}
}