2016-07-25 44 views
0

生病嘗試在這裏具體。所以我創建了最基本的可能的代碼來測試它,我仍然無法獲取數據,請幫助!這裏是我的代碼:如何解析JSON數據與Android上的改造

這是我的本地服務器上的JSON輸出: http://localhost:8080/KokosinjacRestfull/rest/textService/mudriPevci

[{ 「ID」:1, 「子類別」: 「MudriPevci」, 「稱號」:「Mujo我 haso「,」description「:」Krenuli do Grada「,」author「:」luka「,」date「:」2016-06-13「},{」id「:3,」subCategory「:」mudriPevci「, 「頭銜」: 「PERICA」, 「說明」: 「在濟napravio Haos霸 」, 「作家」: 「盧卡」, 「日期」: 「2016年6月13日」}]

文本。等級:

package kokosinjac.com.digiart.koktest.models; 

import com.google.gson.annotations.Expose; 
import com.google.gson.annotations.SerializedName; 


public class Text { 

@SerializedName("id") 
@Expose 
private Integer id; 
@SerializedName("subCategory") 
@Expose 
private String subCategory; 
@SerializedName("title") 
@Expose 
private String title; 
@SerializedName("description") 
@Expose 
private String description; 
@SerializedName("author") 
@Expose 
private String author; 
@SerializedName("date") 
@Expose 
private String date; 

/** 
* No args constructor for use in serialization 
* 
*/ 


/** 
* 
* @param id 
* @param author 
* @param title 
* @param subCategory 
* @param description 
* @param date 
*/ 


/** 
* 
* @return 
* The id 
*/ 
public Integer getId() { 
    return id; 
} 

/** 
* 
* @param id 
* The id 
*/ 
public void setId(Integer id) { 
    this.id = id; 
} 

/** 
* 
* @return 
* The subCategory 
*/ 
public String getSubCategory() { 
    return subCategory; 
} 

/** 
* 
* @param subCategory 
* The subCategory 
*/ 
public void setSubCategory(String subCategory) { 
    this.subCategory = subCategory; 
} 

/** 
* 
* @return 
* The title 
*/ 
public String getTitle() { 
    return title; 
} 

/** 
* 
* @param title 
* The title 
*/ 
public void setTitle(String title) { 
    this.title = title; 
} 

/** 
* 
* @return 
* The description 
*/ 
public String getDescription() { 
    return description; 
} 

/** 
* 
* @param description 
* The description 
*/ 
public void setDescription(String description) { 
    this.description = description; 
} 

/** 
* 
* @return 
* The author 
*/ 
public String getAuthor() { 
    return author; 
} 

/** 
* 
* @param author 
* The author 
*/ 
public void setAuthor(String author) { 
    this.author = author; 
} 

/** 
* 
* @return 
* The date 
*/ 
public String getDate() { 
    return date; 
} 

/** 
* 
* @param date 
* The date 
*/ 
public void setDate(String date) { 
    this.date = date; 
} 

}

阿比interface.class:

包kokosinjac.com.digiart.koktest.retrofit;

import java.util.ArrayList;

import kokosinjac.com.digiart.koktest.models.Text;進口 retrofit2.Call; import retrofit2.http.GET;導入retrofit2.http.Path;

公共接口RetrofitAPInterface {

@GET("rest/textService/{subCategory}") 
Call<ArrayList<Text>> getText(@Path("subCategory") String subCat); 

}

類顯示在手機上的數據(你不需要的一些字符串注意,只看改造的一部分,我; VE使它一樣簡單我可以, subCatData.class:

public static final String BASE_URL ="http://localhost:8080/KokosinjacRestfull/"; 
HashMap<String,String> dataArr; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_sub_cat_data); 
    final TextView tView = (TextView) findViewById(R.id.textView); 
    Intent intent = getIntent(); 
    String urlSecondPartBefore = intent.getStringExtra("passedSubCat"); 
    String urlSecondPartAfter = urlSecondPartBefore.replaceAll("\\s", ""); 
    String urlFirstPart = intent.getStringExtra("passedUrlFirstPart"); 
    String catName = intent.getStringExtra("passedCatName"); 
    String data = null; 
    // TextView test = (TextView) findViewById(R.id.test); 


    Retrofit retrofit = new Retrofit.Builder() 
      .baseUrl(BASE_URL) 
      .addConverterFactory(GsonConverterFactory.create()) 
      .build(); 
    RetrofitAPInterface apiService = retrofit.create(RetrofitAPInterface.class); 
    Call<ArrayList<Text>> call = apiService.getText("mudriPevci"); 
    call.enqueue(new Callback<ArrayList<Text>>() { 
     @Override 
     public void onResponse(Call<ArrayList<Text>> call, Response<ArrayList<Text>> response) { 
      int statusCode = response.code(); 
      ArrayList<Text> textArray = response.body(); 
      for (Text t : textArray){ 
       tView.setText(t.getDescription()); 
      } 
      // Log.i("DATA", "onResponse: "+text.getId()); 
     } 

     @Override 
     public void onFailure(Call<ArrayList<Text>> call, Throwable t) { 

     } 
    }); 

} 

}

我意識到整個數據集都會變成一個簡單的標籤,但它只是用於測試目的。儘管如此,我仍然無法取回任何東西,而且我也沒有收到任何錯誤。幫助將不勝感激。謝謝!

+0

這個解決方案可能會幫助你,請檢查出來http://stackoverflow.com/questions/33338923/how-to-parse-retrofit-json-response?rq=1 –

回答

1

我認爲問題,您的網址如果你是測試您的應用程序與Android模擬器,然後嘗試像"http://10.0.2.2:8080/"但如果你是測試設備,那麼你需要通過你的機器IP地址,如"http://192.143.1.0/"並確保您的設備已連接到您的數據庫所在的機器。

+0

我知道這個問題,因此我正在用我的模擬器進行測試。代碼看起來好嗎? – vibetribe93

+0

yess你的代碼看起來像確定,但是因爲你在你的URL中使用'localhost'可能會遇到問題,試着把''替代'localhost'放在''http://10.0.2.2:8080/「'。 –

+0

謝謝先生,它的工作原理!我試圖直接在瀏覽器中測試它,但它不工作,只有在android工作室。謝謝您的幫助!! – vibetribe93

0

解析Android上的數據JSON我強烈建議使用JSOUP,您可以通過點擊here來找到它,它很簡單,直接,並且用戶友好。非常容易學習。希望這可以幫助你!

+1

謝謝你的回覆,這個問題是因爲我將稍後使用改造來上傳數據,所以我想我需要像api一樣休息。 Thx – vibetribe93

0

的問題可能是你的BASE_URL你BASE_URL更改爲 BASE_URL ="http://<your_local_ip_address>/KokosinjacRestfull/";

+0

我知道這個問題,因此我正在用我的模擬器進行測試。代碼看起來好嗎? – vibetribe93

0

我希望下面的例子可能有助於

 public void startFetching() { 
    mApiManager.getFlowerApi().getFlowers(new Callback<String>() { 
     @Override 
     public void success(String s, Response response) { 
      Log.d(TAG, "JSON :: " + s); 

      try { 
       JSONArray array = new JSONArray(s); 

       for(int i = 0; i < array.length(); i++) { 
        JSONObject object = array.getJSONObject(i); 

        Flower flower = new Flower.Builder() 
          .setCategory(object.getString("category")) 
          .setPrice(object.getDouble("price")) 
          .setInstructions(object.getString("instructions")) 
          .setPhoto(object.getString("photo")) 
          .setName(object.getString("name")) 
          .setProductId(object.getInt("productId")) 
          .build(); 

        mListener.onFetchProgress(flower); 

       } 

      } catch (JSONException e) { 
       mListener.onFetchFailed(); 
      } 


      mListener.onFetchComplete(); 
     } 

     @Override 
     public void failure(RetrofitError error) { 
      Log.d(TAG, "Error :: " + error.getMessage()); 
      mListener.onFetchComplete(); 
     } 
    }); 
} 
0

問題與您的網址的Android不能打你的本地主機即http://localhost:8080/KokosinjacRestfull/REST/textService/mudriPevci

您的服務器在特定的IP和運行。新IP將像 http://192.168.1.1/KokosinjacRestfull/rest/textService/mudriPevci