2012-11-16 5 views
0

업데이트 : 코드는 정상입니다. 그것은 단순히 데이터베이스에 대한 잘못된 포인터였습니다.(URL) 문자열을 JSONObject로 변환 할 수 없습니다.

JSON 데이터를 가져 오기 위해 HTTP 요청을하기 위해 JSONParser 메서드를 사용하고 데이터베이스에서 JSONObject를 반환합니다. JSONObject를 가져 오는 데 문제가 없으며 이름과 전자 메일과 같은 String을 표시하지만 이미지의 URL은 검색하지 않습니다. 아무런 오류가 없지만 빈 필드 만 표시 중입니다. 나는이

String imageURL = "http://mywebsite.com/images/photo1.png"; 

String imageURL = directory.getString(TAG_IMG); 

에서이 라인을 만들 경우

그러나, 그것은 잘 작동합니다.

//URL to make request 
private static final String url_veiw_directory = "http://www.myweb.com/android/include/directory_detail_me.php"; 

private static final String TAG_SUCCESS = "success"; 
private static final String TAG_DIRECTORY = "directory"; 
private static final String TAG_ID = "user_id"; 
private static final String TAG_IMG = "photo"; // Image URLs stored in the DB 
private static final String TAG_NAME = "name"; 
private static final String TAG_EMAIL = "email"; 

//Get JSONObject by httpRequest 
JSONObject json = jsonParser.makeHttpRequest(url_veiw_directory, "GET", params); 

Log.d("my profile", json.toString()); 

if (success == 1) { 

    JSONArray directoryObj = json.getJSONArray(TAG_DIRECTORY); 

    JSONObject directory = directoryObj.getJSONObject(0); 

    //User Image        
    int loader = R.drawable.loader; 

    String imageURL = directory.getString(TAG_IMG);       
    ImageView imagePhoto = (ImageView) findViewById(R.id.photo); 

    ImageLoader imgLoader = new ImageLoader(getApplicationContext());   
    imgLoader.DisplayImage(imageURL, loader, imagePhoto); 

    //User Name and Email       
    TextView txtName = (TextView) findViewById(R.id.name); 
    TextView txtEmail = (TextView) findViewById(R.id.email);        

    txtName.setText(directory.getString(TAG_NAME)); 
    txtEmail.setText(directory.getString(TAG_EMAIL)); 

//Image Loader Class 

public void DisplayImage(String url, int loader, ImageView imageView) 
    { 
     stub_id = loader; 
     imageViews.put(imageView, url); 
     Bitmap bitmap=memoryCache.get(url); 
     if(bitmap!=null) 
      imageView.setImageBitmap(bitmap); 
     else 
     { 
      queuePhoto(url, imageView); 
      imageView.setImageResource(loader); 
     } 
    } 

    private void queuePhoto(String url, ImageView imageView) 
    { 
     PhotoToLoad p=new PhotoToLoad(url, imageView); 
     executorService.submit(new PhotosLoader(p)); 
    } 

    private Bitmap getBitmap(String url) 
    { 
     File f=fileCache.getFile(url); 

     //from SD cache 
     Bitmap b = decodeFile(f); 
     if(b!=null) 
      return b; 

     //from web 
     try { 
      Bitmap bitmap=null; 
      URL imageUrl = new URL(url); 
      HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection(); 
      conn.setConnectTimeout(30000); 
      conn.setReadTimeout(30000); 
      conn.setInstanceFollowRedirects(true); 
      InputStream is=conn.getInputStream(); 
      OutputStream os = new FileOutputStream(f); 
      ImageUtils.CopyStream(is, os); 
      os.close(); 
      bitmap = decodeFile(f); 
      return bitmap; 
     } catch (Exception ex){ 
      ex.printStackTrace(); 
      return null; 
     } 
    } 
+0

같은 생기게 하다? 그것을 기록하고 원하는 문자열과 일치하지 않는 이유를 찾아보십시오. 그런 다음 받아 들일 수있는 방법을 찾으십시오. – TheZ

+0

@TheZ, 미안하지만 나는 일종의 초보자입니다. 세부 사항에 친절하게 조금 설명해 주시겠습니까? – user1781367

+0

서버에서 반환 한 JSON 문자열의 예를 게시 하시겠습니까? – Kyle

답변

1

대부분의 경우 이유는 imageURL 문자열 에테르 비어 있거나 어떤 것을 어쩌면 가능성 (당신이 DB에서 당겨 때문에)이 URL로 사용할 수 없게 폼에 탈출입니다. 내가하는 아마 당신은 그것을 언 이스케이프 또는 일부 문자 대체를 할 수있는 점 값을을 인쇄하여 볼 제안

당신은 볼 수 있습니다 코드`directory.getString (TAG_IMG)을`무엇을 문자열 http%3A//mywebsite.com/images/photo1.png

+0

Bostone, 의견을 보내 주셔서 감사합니다. 그것은 데이터베이스에서 값을 얻지 못하는 것 같아요.하지만 내가 textview 문자열로 검색하면 값을 인쇄 할 수 있기 때문에 유선이야. – user1781367

+0

중단 점을 넣고 문자열 값을 볼 수 있습니까? 더 나은 아직 전체 디렉토리 개체를 볼 수 있습니까? 또한 - db의 데이터 형식은 무엇입니까? JSON을 문자열이나 바이너리로 저장하고 있습니까? – Bostone

+0

예 값을 TextView로 변경하여 값을 인쇄하면 URL 주소가 이미지에 반환됩니다. – user1781367

관련 문제