[英]How can i pass data from class to another class?
I am doing in this code: message receiving,filtering and getting then sending data to a sql server. 我在这段代码中做的:消息接收,筛选,然后将数据发送到sql服务器。 I need the category_id from mysql database. 我需要mysql数据库中的category_id。 Then i will use it in CallAPI as ide . 然后我将在CallAPI中将其用作ide。 I took the data from my mysql database but i couldn't transfer the data one class to another. 我从mysql数据库中获取了数据,但无法将数据从一个类转移到另一个类。 So how can i transfer the data from one class to another? 那么如何将数据从一类转移到另一类?
I solved my problem and updated it i hope it can help to another peoples. 我解决了我的问题并对其进行了更新,希望它可以对其他民族有所帮助。
my smsCame codes: 我的短信验证码:
package com.pvalid.api;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.Request;
import okhttp3.Response;
public class smsCame extends BroadcastReceiver {
private static final String TAG = "MyBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG , "SMS RECEIVEDD");
Bundle bundle = intent.getExtras();
Object[] pdus = (Object[]) bundle.get("pdus");
String format = intent.getExtras().getString("format");
SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[0], format);
String messagea = message.getOriginatingAddress();
String messagesb = message.getMessageBody();
Boolean messagee= messagesb.substring(0, 8).matches("(G-)\\d\\d\\d\\d\\d\\d");
String Code = messagesb.substring(2, 8);
String ide;
String usercode = "Admin";
//i need to POST this lmessage to my php server when sms received
//property is has to be Code:lmessage
// i have a receiver in my url when isset($_POST['Code'])
if (messagee){
try{
ide = new heyAPI(usercode).execute().get();
new CallAPI(usercode, Code, ide).execute();}
catch(Exception e){
ide="11";
new CallAPI(usercode, Code, ide).execute();
}
}
else{
Log.i(TAG,"Didnt match");}
}
private static class heyAPI extends AsyncTask<String, String, String> {
private final OkHttpClient client = new OkHttpClient();
String usercodes;
private heyAPI(String usercode){
usercodes= usercode;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
RequestBody formBody = new FormBody.Builder()
.add("usercode", usercodes) // A sample POST field
.build();
Request request = new Request.Builder()
.url("url-here")
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
Headers responseHeaders = response.headers();
String elem_id= response.body().string();
return elem_id;
}
catch (Exception e){
Log.i(TAG,"Error:"+e);
return null;
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
}
private static class CallAPI extends AsyncTask<String, String, String> {
String emailString;
String commentString;
String id;
private CallAPI(String usercode, String Code,String ide){
emailString = usercode;
commentString = Code;
id=ide;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
OkHttpClient client = new OkHttpClient();
RequestBody formBody = new FormBody.Builder()
.add("usercode", emailString) // A sample POST field
.add("Code", commentString) // Another sample POST field
.add("category_id", id) // Another sample POST field
.build();
Request request = new Request.Builder()
.url("url here") // The URL to send the data to
.post(formBody)
.build();
try {
Response response = client.newCall(request).execute();
return response.body().string();
}catch(IOException e){
Log.i(TAG,"IO exception");
return "";
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
}
}
hi you can send parameters class A to class B is so way... 嗨,您可以将参数A类发送到B类,这样...
you can use constructor like this 你可以这样使用构造函数
public class test {
public test(String p){...}
}
and you can use intent 你可以使用意图
you can use shared preference in 您可以在
learn with this site Shared preference save data from class A and read class B 在此网站上学习共享的首选项保存A类的数据并阅读B类
and you must be careful because when you give data from server thread is different Ui thread ! 并且必须小心,因为从服务器线程提供数据时,Ui线程是其他线程!
private SharedPreferences mPreference;
mPreference = getSharedPreferences("Share", Context.MODE_PRIVATE);
// save data
mPreference.edit()
.putBoolean("test", category_id)
.apply();
// read data
mPreference.getString("test", defaultSTR);
You can use transfer your data in different class by using database , shared preference and intents. 您可以通过使用database,共享的首选项和意图来在不同的类中传输数据。
If You want to transfer data from class A to B by intent then 如果要通过意图将数据从A类传输到B类,则
In class A A班
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("key_name", value);
startActivity(intent);
and In class B for getting the transferred data 在B类中用于获取传输的数据
Intent intent=new getIntent();
String s=intent.getExtras().getString("key_name");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.