我有一个mustache
将service
传递给String of Json
:
GetSubjects.class:
MainActivity
这是我的MainActivity:
public class GetSubjects extends Service {
String URL = "http://webservice.jim.com/YYY/XXXX.asmx";
String Webresponse = "IS NULL ?";
int scode = 1597536485;
String result;
public final static String MY_ACTION = "MY_ACTION";
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(final Intent intent, int flags, final int startId) {
Runnable r = new Runnable() {
@Override
public void run() {
handleStart(intent, startId);
}
};
Thread t = new Thread(r);
t.start();
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
}
void handleStart(Intent intent, int startId) {
try {
Params_GetSubjects param = new Params_GetSubjects(scode);
result = new mGetSubjects().execute(param).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
public class mGetSubjects extends AsyncTask<Params_GetSubjects, String, String> {
String NAMESPACE = "http://tempuri.org/";
String METHOD_NAME = "Get";
String SOAP_ACTION = "http://tempuri.org/Get";
@Override
protected String doInBackground(Params_GetSubjects... params) {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("scode", params[0].Scode);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE conn = new HttpTransportSE(URL);
Object object;
try {
conn.call(SOAP_ACTION, envelope);
//SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
object = envelope.getResponse();
Webresponse = object.toString();
} catch (Exception e) {
e.printStackTrace();
return "NULL";
}finally {
ThreadFinish threadfinish = new ThreadFinish();
threadfinish.start();
return Webresponse;
}
}
}
public class ThreadFinish extends Thread{
@Override
public void run() {
try {
Intent intent = new Intent();
intent.setAction(MY_ACTION);
intent.putExtra("DATAPASSED", result);
sendBroadcast(intent);
}catch (Exception e){
e.printStackTrace();
}finally {
stopSelf();
}
}
}
}
这是我的JsonParer:
public class MainActivity extends AppCompatActivity {
MyReceiver myReceiver;
private RecyclerView.LayoutManager layoutManager;
private DrawerAdapter mDrawerAdapter;
private RecyclerView mRecyclerView;
List<DrawerItem> draweritemList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService(new Intent(MainActivity.this, GetSubjects.class));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id) {
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START);
return true;
case R.id.action_settings:
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onStart() {
myReceiver = new MyReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(GetSubjects.MY_ACTION);
registerReceiver(myReceiver, intentFilter);
super.onStart();
}
@Override
protected void onStop() {
this.unregisterReceiver(myReceiver);
super.onStop();
}
private class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
String datapassed = arg1.getExtras().getString("DATAPASSED");
Log.i("ASDQWRRYGH",datapassed);
draweritemList = JsonParser.parseFeed(datapassed);
for (int i=0;i < draweritemList.size();i++)
{
Log.i("ASDQWRRYGH",draweritemList.get(i).getTitle());
}
}
}
}
经常让我得到服务的结果,但经常让我吼叫错误,我该怎么办?:
答案 0 :(得分:0)
你的Service
执行了三个额外的线程; AsyncTask
和两个Thread
。第一个Thread
正在执行您的AsyncTask
,但由于您在其上调用get()
以将返回值分配给return
String
,因此它已被屏蔽。当网络事务在AsyncTask
中完成时,它会启动另一个Thread
来发送广播,这发生在doInBackground()
可以返回以设置return
{{的值之前1}},当它仍然为空时,最终会被广播。
最简单的解决方法是将广播移到String
行之后,然后摆脱result = ...
类。
但是,最好在单个线程中按顺序执行所有操作;在ThreadFinish
或执行AsyncTask
的{{1}}。