0

I have layout xml name main.xml and i have main.java class in that class now i send network call and get data from rest service.But now i want to separate network call in anohther class.How can i do that.

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <Button
        android:id="@+id/callsubclass"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Call sub-class" />
    <TextView
        android:id="@+id/result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

main.java

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buttonCallSubClass = (Button)findViewById(R.id.callsubclass);
        textResult = (TextView)findViewById(R.id.result);

       // i can set network call here and update TExtview

       textResult .setText(result);
    }

rest service response ={"id":1958,"content":"Hello, World!"}

my separate nework.java class

String url1 = String.format("http://rest-service.guides.spring.io/greeting");
        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
                url1, null,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d("Test", response.toString());
                        String token="a";
                        String id="";
                        String result="";
                        try {
                            id= response.get("id").toString();
                            result= response.get("content").toString();
                        }catch (JSONException e){
                            e.printStackTrace();
                            Toast.makeText(getApplicationContext(),
                                    "Error: " + e.getMessage(),
                                    Toast.LENGTH_LONG).show();
                        }


                    }

now i want to do network call in separate class give a solution.

BNK
  • 23,994
  • 8
  • 77
  • 87
Lakshan
  • 208
  • 1
  • 4
  • 17
  • 1
    where is the problem in this? – Tushar Saha Oct 10 '15 at 08:22
  • But now i want to separate network call in anothher class. => Dude as you mentioned above,you already have a separate network class! – Paresh Mayani Oct 10 '15 at 08:44
  • But i cant update my UI(xml) in separated class from the result of rest service – Lakshan Oct 10 '15 at 09:08
  • 1
    Read [my answer here](http://stackoverflow.com/questions/32627089/post-request-json-file-passing-string-and-wait-for-the-response-volley/32627293#32627293). Hope this helps! – BNK Oct 10 '15 at 10:21
  • 1
    And [one more here](http://stackoverflow.com/questions/32551668/android-volley-static-vs-object/32642986?noredirect=1#comment53137764_32642986) – BNK Oct 10 '15 at 10:25
  • @BNK i read your post but i cant understand how can i apply it to my project.can you explain me how it apply for my project. – Lakshan Oct 10 '15 at 11:11
  • 1
    Ok. Use and interface or callbacks, Override in your main activity your custom método/ metods, pass your callback to your rest callback and ok success Override your custom method of callback, so in your activity Override you can update te ui. Is the best way – Max Pinto Oct 10 '15 at 12:03
  • @MaxPinto can you give me a example with my above codes. – Lakshan Oct 10 '15 at 12:38
  • Read my sample code in the two links I sent above. It's easy to understand if you already used Volley. – BNK Oct 10 '15 at 12:42
  • Done it.Thanks BNK,Max Pinto and others – Lakshan Oct 10 '15 at 17:37
  • Glad it could help, you should post your solution as an answer to this question for other men's reference – BNK Oct 10 '15 at 20:58

0 Answers0