Ben String of Json
MainActivity
biletlerinde service
var:Android - Yayın alma hatası var mı?
GetSubjects.class: Burada
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();
}
}
}
}
benim MainActivity geçerli:
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());
}
}
}
}
Ve işte benim JsonParer geçerli:
public class JsonParser {
public static List<DrawerItem> parseFeed(String content) {
try {
JSONObject jsonRootObject = new JSONObject(content);
JSONArray jsonArray = jsonRootObject.optJSONArray("Rows");
List<DrawerItem> draweritemList = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
DrawerItem draweritem = new DrawerItem();
draweritem.setId(obj.getString("Id"));
draweritem.setTitle(obj.getString("Title"));
draweritemList.add(draweritem);
}
return draweritemList;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
}
sıklıkla İçeri yürütme üç ek konuları var
Konularınızın tümünü karışık hale getiriyorsunuz, ve sonuç alınmadan yayın yapıyorsunuz. ThreadFinish sınıfına gerçekten ihtiyacınız yok. En basit düzeltme, yayını "result = ..." satırından sonra taşımaktır. Gerçekten de, herşeyi tek bir iş parçacığına koyabilirsin ya da yapmalıydın; ya AsyncTask 'ya da' Runnable'ınızı çalıştıran 'Thread'. –
Çok çok teşekkürler, benim sorunum senin yardımınla çözüldü.Oyun için yanıtını ver. Çok teşekkürler. –