public class sendMesgTask extends AsyncTask<String, Void, Void> {
String msg = "";
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(String... params) {
try {
String result = sendMsg(params[0]);
msg = getResult(result);
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
if (msg.equals("1")) {
}
}
public String sendMsg(String message) throws IOException, JSONException {
try {
String sendmsgurl = "http://api.lociiapp.com/api/message/sendmessage?member_id="
+ 452
+ "&to_id="
+ 452
+ "&message="
+ message
+ "&Notificationtype="
+ "3"
+ "¬ificationId="
+ (int) (Math.random() * 100000000)
+ "¬ify_status="
+ "false";
Log.e("Valueeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", sendmsgurl);
System.out.println("Valueeeeeeeeeeeeeeeeeee" + sendmsgurl);
URL url = new URL(sendmsgurl);
URLConnection urlcon = url.openConnection();
jsonResp = convertToString(urlcon.getInputStream());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonResp;
}
public String convertToString(InputStream is) {
if (is != null) {
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(new InputStreamReader(
is, "UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} catch (UnsupportedEncodingException e) {
// TODO: handle exception
} catch (IOException e) {
} finally {
try {
is.close();
} catch (IOException e) {
}
}
return writer.toString();
} else {
return "";
}
}
private String getResult(String response) {
String test = "";
try {
JSONObject json = (JSONObject) new JSONTokener(response)
.nextValue();
test = json.getString("responseCode");
} catch (JSONException e) {
}
return test;
}
这是用于发送消息的类asynk任务我正在调用此asynk任务,就像这个新的sendMesgTask()。执行(&#34; Rjected&#34;);
我想像这样调用asynk任务:
new sendMesgTask().execute(452,452,Rejected,3,(int) (Math.random() * 100000000),false);
请帮助我如何上课,以便我可以这样打电话
答案 0 :(得分:0)
最好使用asynctask的构造函数,如下所示:
public class sendMesgTask extends AsyncTask<String, Void, Void> {
String msg = "";
int i;
String j;
public sendMesgTask(int i, String j){
this.i = i;
this.j = j;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(String... params) {
try {
String result = sendMsg(params[0]);
msg = getResult(result);
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
现在,您可以在整个asynctask类中使用i和j变量。
并打电话:
new sendMesgTask(5,"test").execute();
答案 1 :(得分:0)
为什么不使用像这样的构造函数方法
public class sendMesgTask extends AsyncTask<String, Void, Void> {
int num1;
int num2;
String accept_reject;
int num3;
int randum_number;
boolean bool;
public sendMesgTask(int num1, int num2, String accept_reject, int num3, int randum_number, boolean bool){
this.num1=num1;
this.num2= num2;
this.accept_reject = accept_reject;
this.num3 = num3;
this.randum_number=randum_number;
this.bool=bool;
}
.
.
//Your rest of code remains same
.
.
.
}
像这样打电话
new sendMesgTask(452,452,Rejected,3,(int) (Math.random() * 100000000),false).execute();
编辑:1
new sendMesgTask(452,452,"Rejected",3,(int) (Math.random() * 100000000),false).execute();
好的,有你的问题
现在在你的doInBackground方法调用中就像这样
String result = sendMsg(sendeid,recciveid,msg,type,status);
而不是
String result = sendMsg(params[0]);
答案 2 :(得分:0)
public class sendMesgTask extends AsyncTask<Object, Void, String> {
String msg = "";
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Object... params) {
try {
String result = sendMsg(params[0]);
msg = getResult(result);
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
if (msg.equals("1")) {
}
}
public String sendMsg(String message) throws IOException, JSONException {
try {
String sendmsgurl = "http://api.lociiapp.com/api/message/sendmessage?member_id="
+ 452
+ "&to_id="
+ 452
+ "&message="
+ message
+ "&Notificationtype="
+ "3"
+ "¬ificationId="
+ (int) (Math.random() * 100000000)
+ "¬ify_status="
+ "false";
Log.e("Valueeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", sendmsgurl);
System.out.println("Valueeeeeeeeeeeeeeeeeee" + sendmsgurl);
URL url = new URL(sendmsgurl);
URLConnection urlcon = url.openConnection();
jsonResp = convertToString(urlcon.getInputStream());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonResp;
}
public String convertToString(InputStream is) {
if (is != null) {
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(new InputStreamReader(
is, "UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} catch (UnsupportedEncodingException e) {
// TODO: handle exception
} catch (IOException e) {
} finally {
try {
is.close();
} catch (IOException e) {
}
}
return writer.toString();
} else {
return "";
}
}
private String getResult(String response) {
String test = "";
try {
JSONObject json = (JSONObject) new JSONTokener(response)
.nextValue();
test = json.getString("responseCode");
} catch (JSONException e) {
}
return test;
}