2015-10-12 69 views
1

我在android工作室中的一段代碼出現問題,這段代碼一直工作可靠幾個星期。 每當我試圖通過本地主機接收JSON(在模擬器或設備上調試和連接通過WiFi)我得到的錯誤信息:離子無法從本地主機解析Json

com.google.gson.JsonParseException:無法解析JSON

然而,如果我連接到遠程服務器用於一個JSON回聲如:

http://echo.jsontest.com/insert-key-here/insert-value-here/key/value

是沒有問題的。

我已經驗證了JSON是有效的,就像我之前說過的那樣,但我已經在網上反覆檢查過它,我甚至創建了一個只有一個按鈕和一個請求的新的裸骨應用程序,但仍然有同樣的問題。

的代碼如下:

public static class PlaceholderFragment extends Fragment { 

    public static final String ACCOUNT_VIEW_URL = "http://192.168.0.10/~user/tests/accountEcho.php"; //"http://echo.jsontest.com/insert-key-here/insert-value-here/key/value"; 
    Button button; 

    private static final String ARG_SECTION_NUMBER = "section_number"; 


    public static PlaceholderFragment newInstance(int sectionNumber) { 
     PlaceholderFragment fragment = new PlaceholderFragment(); 
     Bundle args = new Bundle(); 
     args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    public PlaceholderFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_home_screen, container, false); 
     button = (Button) rootView.findViewById(R.id.button); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       netWork(); 
      } 
     }); 
     return rootView; 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     ((HomeScreenActivity) activity).onSectionAttached(
       getArguments().getInt(ARG_SECTION_NUMBER)); 
    } 


    void netWork() { 
     Ion.with(getActivity()) 
       .load(ACCOUNT_VIEW_URL) 
       .asJsonObject() 
       .setCallback(new FutureCallback<JsonObject>() { 
        @Override 
        public void onCompleted(Exception e, JsonObject result) { 

         if (null == e) { 
          // Log.v("onCompleted", "not null"); 
          Log.v("result", ""+result+""); 
         } else { 
          // Log.v("onCompleted", "null"); 
          Log.v("on completed", "" + e + ""); 

         } 
         //TODO:::: JSON STRUCTURE - LOAD DP INTO VIEW 
        } 
       }); 

    } 
} 

}

和JSON的反應是:

{ 
     "accType": "2", 
     "firstname": "duracell", 
     "lastname": "battery" 
     "house_num": "122", 
     "addL1": "Brick Lane", 
     "addL2": "", 
     "city": "Middx" 

} 

另外,如果我訪問我的計算機上的任何其他應用程序相同的本地主機地址(visualJSON,chromeAdvancedRestClient,safari,firefox等)。它工作正常,響應如預期。

任何人都可以告訴我怎麼回事?

感謝

回答

0

如果你的JSON是在服務器上,您通過HTTP請求進行訪問,你需要來解析內容,不是用頭整個文件。

嘗試做一個HTTP客戶端,使用getResponse.getEntitygetResponse.getContent,然後您的解析器在其中之一。

我從here複製那個。

1

我這是怎麼解決的是:

第1步:

確保您有一個object上課,objects必須擁有與您JSON對象相同的名稱。

public class Tester { 

    private String accType; 
    private String firstname; 
    private String lastname; 
    private String addL1; 
    private String addL2; 
    private String city; 


    public String getAccType() { 
     return accType; 
    } 

    public void setAccType(String accType) { 
     this.accType = accType; 
    } 

    public String getFirstname() { 
     return firstname; 
    } 

    public void setFirstname(String firstname) { 
     this.firstname = firstname; 
    } 

    public String getLastname() { 
     return lastname; 
    } 

    public void setLastname(String lastname) { 
     this.lastname = lastname; 
    } 

    public String getAddL1() { 
     return addL1; 
    } 

    public void setAddL1(String addL1) { 
     this.addL1 = addL1; 
    } 

    public String getAddL2() { 
     return addL2; 
    } 

    public void setAddL2(String addL2) { 
     this.addL2 = addL2; 
    } 

    public String getCity() { 
     return city; 
    } 

    public void setCity(String city) { 
     this.city = city; 
    } 
} 

步驟2:

然後我檢索JSON數據作爲一個列表,但如果假設,我有一個項目,我只是索引每個元件如下

 Ion.with(getBaseContext()) 
         .load(url_) 
         .progressDialog(pd) 
         .as(new TypeToken<List<Tester>>() { 
         }) 
         .setCallback(new FutureCallback<List<Tester>>() { 

          @Override 
          public void onCompleted(Exception e, List<Tester> itemList) { 

           try { 


            String firstname= itemList.get(0).getFirstname(); 

//DO the same to other elements 

     Log.v("result", "Firstname :"+firstname+""); 


            pd.dismiss(); 


           } 

           catch (Exception ex){ 

            Toast.makeText(getApplicationContext(),"Please check your Internet connection",Toast.LENGTH_LONG).show(); 
           } 






          } 



         }); 


     }