Java 将JSONArray发布到php
我试图从我的手机中获取联系人,但我想将数据发布到php服务以存储在数据库中。检索之后,我创建了一个具有名称-值对的数组,即代码段Java 将JSONArray发布到php,java,android,Java,Android,我试图从我的手机中获取联系人,但我想将数据发布到php服务以存储在数据库中。检索之后,我创建了一个具有名称-值对的数组,即代码段 public void getContacts() throws IOException, JSONException { List<String> phnnumbers = new ArrayList<String>(); List<String> names = new ArrayL
public void getContacts() throws IOException, JSONException {
List<String> phnnumbers = new ArrayList<String>();
List<String> names = new ArrayList<String>();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
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);
names.add(name);
// get the phone number
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println("phone" + phone);
phnnumbers.add(phone);
}
}
}
Log.d("NAMES",name.toString());
// registerContacts(this,URL,names,phnnumbers);
}
}
如何格式化它,以便将其存储在数据库中。我的php端
function SynchContacts() {
$phone = $this->input->post('phone');
$name = $this->input->post('name');
for ($i = 0; $i < count($phone); $i++) {
$data = array(
'name' => $name[$i],
'phone' => $phone[$i]
);
$this->saveData('phonebook', $data);
}
}
函数SynchContacts(){
$phone=$this->input->post('phone');
$name=$this->input->post('name');
对于($i=0;$i有什么建议吗?在PHP端,您需要以下JSON:
[
{
"name": "Chacha Kori",
"phone": "+123456987"
},
{
"name": "Gabuli Somi",
"phone": "+123456987"
},
{
"name": "Geto Somi",
"phone": "+123456987"
}
]
当您管理GsonBuilder
为您提供这样的JSON时(对不起,我帮不上忙),您可以将其发布到PHP服务中
然后,在PHP中:
// Get the json variable which contains our JSON string
$json = $this->input->post('json');
// Decode JSON into PHP Array
$contacts = json_decode($json, true);
// Iterate through the collection of phone/names
foreach ($contacts as $row) {
$data = array(
'name' => $row['name'],
'phone' => $row['phone'],
);
$this->saveData('phonebook', $data);
}
另请参见:我的建议是不要使用NameValuePair,因为在棒棒糖版本上面,NameValuePair已被弃用,所以最好使用Arraylist
public void getContacts() {
List<String> phnnumbers = new ArrayList<String>();
List<String> names = new ArrayList<String>();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
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);
names.add(name);
// get the phone number
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println("phone" + phone);
phnnumbers.add(phone);
}
pCur.close();
}
}
}
}
联系方式如下:
public static JSONObject registerTask(Context ctx, String sUrl, List<String> names, List<String> phoneNums) throws JSONException, IOException {
JSONObject request = new JSONObject();
request.putOpt("names", names);
request.putOpt("phoneNums", phoneNums);
sUrl = sUrl + "yourapiname";
return post(sUrl, request.toString());
}
private static String sanitizeJSONBody(String body) {
if (body.contains("password")) {
body = body.replaceAll("\"password\":\"[^\"]+\"",
"\"password\":******");
}
if (body.contains("newPassword")) {
body = body.replaceAll("\"newPassword\":\"[^\"]+\"",
"\"newPassword\":******");
}
return body;
}
publicstaticjsonobjectregistertask(contextctx,stringsurl,List name,List phoneNums)抛出JSONException,IOException{
JSONObject请求=新建JSONObject();
请求。putOpt(“名称”,名称);
请求。putOpt(“phoneNums”,phoneNums);
sUrl=sUrl+“yourapiname”;
返回post(sUrl,request.toString());
}
私有静态字符串sanitizeJSONBody(字符串体){
if(body.contains(“密码”)){
body=body.replaceAll(“\”密码\“:\”[^\“]+\”,
“\”密码\“:*******”;
}
if(body.contains(“newPassword”)){
body=body.replaceAll(“\”新密码\“:\”[^\“]+\”,
“\”新密码\“:*******”;
}
返回体;
}
您的PHP代码如下所示:
$inputJSON = file_get_contents('php://input');
$input = json_decode($inputJSON, TRUE);
$nameArray = array($input['names']);
$phoneNumArray = array($input['phoneNums']);
for($i=0;i<count($nameArray);$i++){
$data = array(
'name' => $nameArray[$i],
'phone' => $phoneNumArray[$i],
);
$this->saveData('phonebook', $data);
}
$inputJSON=file\u get\u contents('php://input');
$input=json_decode($inputJSON,TRUE);
$nameArray=array($input['names']);
$phoneNumArray=array($input['phoneNums']);
对于($i=0;i$nameArray[$i],
“phone”=>$phoneNumArray[$i],
);
$this->saveData('phonebook',$data);
}
@B Kumar,谢谢,这个方法有什么作用;我已经添加了sanitizeJSONBody()的方法实现;,实际上,这个方法将用于在Log.v中显示请求和响应(如果出于安全原因有密码字段,则替换***)。@B Kumar,到目前为止,我可以看到正文输出,但它似乎被截断为{“phone”:“[+123,+456,789,而不是{“phone”:“[+123,+456,789]”,原因可能是什么?@Alphy我不确定您的问题,请您简要解释一下。@B Kumar,在做了您建议的更改后,我调用了方法registerContacts(this,URL,names,phnnumbers);根据您的建议,在php方面,我无法获得JSON,因为getContacts()方法似乎在完全创建数组之前停止,因此发送到post方法的输出看起来像{“phone”:“[+123,+456,789,而不是{“phone”:“[+123,+456,789]”。请注意,没有右括号和coulibracket
public static JSONObject registerTask(Context ctx, String sUrl, List<String> names, List<String> phoneNums) throws JSONException, IOException {
JSONObject request = new JSONObject();
request.putOpt("names", names);
request.putOpt("phoneNums", phoneNums);
sUrl = sUrl + "yourapiname";
return post(sUrl, request.toString());
}
private static String sanitizeJSONBody(String body) {
if (body.contains("password")) {
body = body.replaceAll("\"password\":\"[^\"]+\"",
"\"password\":******");
}
if (body.contains("newPassword")) {
body = body.replaceAll("\"newPassword\":\"[^\"]+\"",
"\"newPassword\":******");
}
return body;
}
$inputJSON = file_get_contents('php://input');
$input = json_decode($inputJSON, TRUE);
$nameArray = array($input['names']);
$phoneNumArray = array($input['phoneNums']);
for($i=0;i<count($nameArray);$i++){
$data = array(
'name' => $nameArray[$i],
'phone' => $phoneNumArray[$i],
);
$this->saveData('phonebook', $data);
}