2016-11-19 5 views
-1

私はサーバーからJsonの応答を取得しようとしており、次のアクティビティのarraylistとしてそれを渡します。これはボレーでうまくいきましたが、私は改造を介して回答を得ることができません。ここに私のコードです。サーバからのjsonレスポンスを取得し、Retrofitを使用してArraylistに保存するにはどうすればいいですか?

public class MainActivity extends AppCompatActivity { 


public static final String ROOT_URL = "http://#####.####/"; 

String username; 
String password; 

private Button makerequest; 

private ProgressDialog pdialog; 

private EditText edittextusername; 
private EditText edittextpassword; 

private String DataArray[] = new String[15]; 

private ArrayList<Result> dataarr = new ArrayList<>(); 

int i=0; 






@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    makerequest = (Button) findViewById(R.id.btnObjRequest); 


    edittextpassword = (EditText) findViewById(R.id.editpassword); 
    edittextusername = (EditText) findViewById(R.id.editusername); 

    pdialog = new ProgressDialog(this); 
    pdialog.setMessage("wait kar be..."); 
    pdialog.setCancelable(false); 


    makerequest.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      makejsonobjectrequest(); 
     } 
    }); 


} 


private void makejsonobjectrequest(){ 


    username = edittextusername.getText().toString().trim(); 
    password = edittextpassword.getText().toString().trim(); 


    RestAdapter adapter = new RestAdapter.Builder() 
      .setEndpoint(ROOT_URL) 
      .build(); 


    //creating object of our interface 
    DataApi data = adapter.create(DataApi.class); 

    //defining the method. 
    data.getposition(username, password, new Callback<ArrayList<Result>>() { 


       @Override 
       public void success(ArrayList<Result> list, Response response) { 




        dataarr= list; 



        Bundle b = new Bundle(); 
        b.putSerializable("array", dataarr); 
        Intent intent = new Intent(getBaseContext(), Display.class); 
        intent.putExtras(b); 
        startActivity(intent); 


       } 

       @Override 
       public void failure(RetrofitError error) { 

        Toast.makeText(getBaseContext(), error.toString(),Toast.LENGTH_SHORT).show(); 


       } 
      }); 

} 
} 
+0

エラーは次のとおりです。retrofit.RetrofitError:com.google.gson.JsonSyntaxException:java.lang.illegalStateException:BEGIN_ARRAYが必要ですが、行1の列2のパス$でBEGIN_OBJECTでした。 –

+0

ここにポストマンを使用してJSONを応答します。このエラーはbcoz jsonが単純なオブジェクトから始まりますが、ArrayListに取得しようとしています。あなたのような同じ問題を見ているhttp://stackoverflow.com/questions/40689691/how-do-i-store-the-json-response-from-my-server-to-an-arraylist-and-pass-次へ/ 40690138#40690138 – sushildlh

答えて

0

を使用してJSONレスポンスを取得します。このリンクをクリックしてください。

私はあなたがアプリケーションが正しく解析できるように、サーバーから来る応答が正しい形式であることを最初に確認する必要があると思います。

アンドロイドスタジオを使用している場合は、リクエストを送信した後、アンドロイドモニターでそのリクエストに使用されたURLとURLを確認できます。https://www.getpostman.com/のようなアプリケーションを使用して、正しいかどうか確認してくださいサーバーからの応答。

これが役に立ちます。

関連する問題