我创建了一个Api,用于在数据库中添加名为sendMultipleInvites的邀请。现在我想在android中实现这个API。我正在尝试创建一个AsyncTask来调用api。我有帮助类连接到http服务器。
sendMultipleInvites.php
<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
ini_set('display_errors', '1');
require 'Invitation.php';
$jsonText = file_get_contents('php://input');
if(empty($jsonText))
{
$response = array("status"=>-2,"message"=>"Empty request");
die(json_encode($response));
}
$json = json_decode($jsonText);
$response = array();
foreach ($json as $jsn) {
foreach($jsn as $j)
{
$sender_id = $j -> sender_id;
$date= $j -> date;
$invitee_no = $j -> invitee_no;
$status = $j -> status;
$user_name = $j -> user_name;
$invitation = new Invitation($sender_id,$date,$invitee_no,$status,$user_name);
$response[] = $invitation->sendMultipleInvites();
}
}
echo(json_encode($response));
?>
serverRequest类:
public class ServerRequest {
String api;
JSONObject jsonParams;
public ServerRequest(String api, JSONObject jsonParams) {
this.api = api;
this.jsonParams = jsonParams;
}
public JSONObject sendRequest() {
try {
URL url = new URL(api);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json");
con.setDoOutput(true);
con.setDoInput(true);
OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());
writer.write(jsonParams.toString());
writer.close();
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
StringBuilder sb = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line = "";
while ( (line = reader.readLine()) != null ){
sb.append(line);
}
reader.close();
Log.d("ServerResponse", new String(sb));
return new JSONObject(new String(sb));
} else {
throw new UnexpectedServerException("Unexpected server exception with status code : "+responseCode);
}
} catch (MalformedURLException me) {
me.printStackTrace();
return Excpetion2JSON.getJSON(me);
} catch(IOException ioe) {
ioe.printStackTrace();
return Excpetion2JSON.getJSON(ioe);
} catch(UnexpectedServerException ue) {
ue.printStackTrace();
return Excpetion2JSON.getJSON(ue);
} catch (JSONException je) {
je.printStackTrace();
return Excpetion2JSON.getJSON(je);
}
}
public ServerRequest(String api) {
this.api = api;
}
}
AsyncTask:
public class SendMultipleInvitesAsyncTask extends AsyncTask<Map<String, String>, Void, JSONObject> {
private Context context;
JSONArray array = new JSONArray();
public SendMultipleInvitesAsyncTask(Context context)
{
this.context = context;
}
@Override
protected JSONObject doInBackground(Map<String, String>... params) {
try {
String api = context.getResources().getString(R.string.server_url) + "contactsapi/sendInvite.php";
Map2JSON mjs = new Map2JSON();
JSONObject jsonParams = mjs.getJSON(params[0]);
ServerRequest request = new ServerRequest(api, jsonParams);
return request.sendRequest();
} catch(JSONException je) {
return Excpetion2JSON.getJSON(je);
}
}
@Override
protected void onPostExecute(JSONObject jsonObject) {
super.onPostExecute(jsonObject);
try {
int result = jsonObject.getInt("result");
String message = jsonObject.getString("message");
if ( result == 1 ) {
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
//Code for having successful result for register api goes here
} else {
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
//Code when api fails goes here
}
} catch(JSONException je) {
je.printStackTrace();
Toast.makeText(context, je.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
我尝试使用哈希映射传递数据。但它给出了一个错误。
错误:
<br /><b>Warning</b>: Invalid argument supplied for foreach() in <b>/var/www/html/contactsapi/sendInvite.php</b> on line <b>30</b><br /><br /><b>Warning</b>: Invalid argument supplied for foreach() in <b>/var/www/html/contactsapi/sendInvite.php</b> on line <b>30</b><br /><br /><b>Warning</b>: Invalid argument supplied for foreach() in <b>/var/www/html/contactsapi/sendInvite.php</b> on line <b>30</b><br /><br /><b>Warning</b>: Invalid argument supplied for foreach() in <b>/var/www/html/contactsapi/sendInvite.php</b> on line <b>30</b><br />[]
09-12 09:27:13.311 4177-4200/com.example.siddhi.contactsdrivers W/System.err: org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
的活动:
public class SendInviteActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send_invite);
HashMap<String, String> params = new HashMap<String, String>();
params.put("date", "17/08/2016");
params.put("invitee_no","14788452");
params.put("status","1");
params.put("user_id","13");
new SendMultipleInvitesAsyncTask(SendInviteActivity.this).execute(params);
}
}
如何从异步任务发送数组?任何人都可以帮忙。谢谢..
编辑:
输入json:
{
"date":"17\/08\/2016",
"invitee_no":"14788452",
"status":"1",
"user_id":"13"
}