我使用的是代码运行良好的soap,但我需要在其他类中使用一些常用方法,并在所有活动中使用它。 但我不知道如何分离方法并使用它。请指导我解决这个问题。这是我的代码..
class A extend Activity{
private String sessionId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.sortfilterclick);
new CommonElement().execute();
}
class CommonElement extends AsyncTask<String, String, String> {
ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(CommonElement.this);
dialog.show();
dialog.setCancelable(false);
}
@Override
protected String doInBackground(String... args) {
try {
// these are all common
SoapSerializationEnvelope env = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
env.dotNet = false;
env.xsd = SoapSerializationEnvelope.XSD;
env.enc = SoapSerializationEnvelope.ENC;
HttpTransportSE androidHttpTransport = new HttpTransportSE(Constants.API_URL);
sessionId = Utils.readPreferences(CommonElement.this,Constants.SESSION_ID, null);
if (sessionId == null) {
SoapObject request = new SoapObject(Constants.NAMESPACE, "login");
request.addProperty("username", "Clothing");
request.addProperty("apiKey", "Clothing");
env.setOutputSoapObject(request);
androidHttpTransport.call("", env);
Object result = env.getResponse();
sessionId = result.toString();
Utils.savePreferences(CommonElement.this,
Constants.SESSION_ID, sessionId);
}// till this it's common
//here json reuest datas varies in json.put()...
SoapObject requests = new SoapObject(Constants.NAMESPACE, "call");//these are common
requests.addProperty("sessionId", sessionId);//these are common
requests.addProperty("resourcePath","sortap.Action"); //this will change for every property
JSONObject json = new JSONObject();// these will change
json.put("page", "1");
json.put("limit", "10");
json.put("name", sortName);
json.put("order", sortOrder);
json.put("id", "3");
json.put("cate_id", "4");
String params = json.toString();
requests.addProperty("args", params);
env.setOutputSoapObject(requests);
androidHttpTransport.call("", env);
Object results = env.getResponse();
//based on various request and response this varies.
if (results.toString() != null) {
JSONObject jsono = new JSONObject(results.toString());
JSONArray jarray = jsono.getJSONArray("results");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
String id = object.getString("id");
String productName = object.getString("product_name");
String imageUrl = object.getString("image_url");
int productPrice = object.getInt("price");
}
}
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
dialog.cancel();
}
========================== 这是我做的改变.. ==============================
public final class SoapCommon {
public static String sessionId;
public static String id;
public static String SessionIdNull(){
try{
SoapSerializationEnvelope env = new SoapSerializationEnvelope(
SoapSerializationEnvelope.VER11);
env.dotNet = false;
env.xsd = SoapSerializationEnvelope.XSD;
env.enc = SoapSerializationEnvelope.ENC;
HttpTransportSE androidHttpTransport = new HttpTransportSE(
Constants.API_URL);
SoapObject request = new SoapObject(
Constants.NAMESPACE, "login");
request.addProperty("username", "Clothing");
request.addProperty("apiKey", "Clothing");
env.setOutputSoapObject(request);
androidHttpTransport.call("", env);
Object result = env.getResponse();
sessionId = result.toString();
}
catch (Exception e) {
e.printStackTrace();
}
return sessionId;
}
public static final void noSession(){
try{
SoapSerializationEnvelope env = new SoapSerializationEnvelope(
SoapSerializationEnvelope.VER11);
env.dotNet = false;
env.xsd = SoapSerializationEnvelope.XSD;
env.enc = SoapSerializationEnvelope.ENC;
HttpTransportSE androidHttpTransport = new HttpTransportSE(
Constants.API_URL);
SoapObject requests = new SoapObject(
Constants.NAMESPACE, "call");
requests.addProperty("sessionId", sessionId);
env.setOutputSoapObject(requests);
androidHttpTransport.call("", env);
Object results = env.getResponse();
androidHttpTransport.call("", env);
Object result = env.getResponse();
sessionId = result.toString();
}
catch (Exception e) {
e.printStackTrace();
}
}
并在另一项活动中:
void doInBackground {
try {
// here
sessionId = Utils.readPreferences(
SortFilterPopupActivity.this,
Constants.SESSION_ID, null);
if (sessionId == null) {
// I splitted and used but don't know how to use here.
Utils.savePreferences(
CommonElement.this,
Constants.SESSION_ID, sessionId);
}
// same here too
env.setOutputSoapObject(requests);
androidHttpTransport.call("", env);
Object results = env.getResponse();
Log.e("Sort results", results.toString());
if (results.toString() != null) {
........
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
但在这个问题上受到了打击。
答案 0 :(得分:1)
创建一个类并在其中定义一个方法..
第1步:创建课程&amp;定义方法..
示例:
public class CommonUtils {
// always create public method and define it a static method
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager
.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
}
在这里,你也可以定义公共变量,它可以在整个应用程序中使用..
第2步:在另一个类中使用它。
语法: YOURCLASS.YOURMETHOD(PARAMETERS);
示例:
if (CommonUtils.isNetworkAvailable(mContext)) {
//do your code...
//i have defined it in IF condition because my method returns a value.. you can define it `void`...
}
答案 1 :(得分:0)
如果方法可以是无实例的,那么创建一个像这样的utilytie类:
public Utils {
public static void doSomethingUsefull(Param p) {
}
}
然后你可以在没有任何其他类的实例的情况下使用这个类的方法:
...
Utils.doSomethingUfesul(p);
...
其他明智的方法是使用构造函数创建一个真正的类,依此类推。
public StatefullUtils {
public StatefullUtils(Param p) {
this.p = p;
...
}
public void doSomethingUsefull() {
p++;
...
}
}
然后您可以通过以下方式在其他地方使用此课程:
StatefullUtils utils = new StatefullUtils(p);
utils.doSomethingUsefull();
希望有所帮助。