2016-04-16 42 views
0

我是Android開發人員的新手,我一直在努力研究如何在向Mysql數據庫發出HTTP請求之後繪製一些圖形。我已經研究過SyncTask,自定義視圖和onDraw方法,但我必須缺少一些東西,因爲我無法讓它工作。基本上,http請求從db中選擇一些記錄(我得到了這個工作),我需要用數據創建一個圖。 http請求是一個異步進程,所以到http請求完成時,onDraw方法在沒有數據的情況下執行,這就是爲什麼我開始研究SyncTask但我無法設法傳遞存儲要繪製數據的數組來自onPostExecute方法。任何幫助將不勝感激,如果你可以指出一些很好的示例代碼。謝謝如何在httprequest之後實現對數據庫的提取

+0

向我們顯示您的代碼。 –

回答

0

下面是我在網上找到的一些代碼,我一直在努力使它工作,到目前爲止我可以獲取數據,以簡化它我只使用字段位置,並且我想繪製文本位置:

public class TestFragment extends Fragment { 

    private static final String TAG = "AsyncTestFragment"; 

    // get some fake data 
    private static final String TEST_URL     = "http://jsonplaceholder.typicode.com/comments"; 
    //private static final String TEST_URL     = "http://localhost/~juan/A_get_tanks.php"; 
    private static final String ACTION_FOR_INTENT_CALLBACK = "THIS_IS_A_UNIQUE_KEY_WE_USE_TO_COMMUNICATE"; 

    ProgressDialog progress; 

    @Override 
    public View onCreateView(LayoutInflater inflater, 
          ViewGroup container, 
          Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.fragment_test, container, false); 
    } 


    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     getContent(); 
    } 

    private void getContent() { 
     // the request 
     try { 
      HttpGet httpGet = new HttpGet(new URI(TEST_URL)); 
      RestTask task = new RestTask(getActivity(), ACTION_FOR_INTENT_CALLBACK); 
      task.execute(httpGet); 
      progress = ProgressDialog.show(getActivity(), "Getting Data ...", "Waiting For Results...", true); 
     } 
     catch (Exception e) { 
      Log.e(TAG, e.getMessage()); 
     } 

    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     getActivity().registerReceiver(receiver, new IntentFilter(ACTION_FOR_INTENT_CALLBACK)); 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     getActivity().unregisterReceiver(receiver); 
    } 

    /** 
    * Our Broadcast Receiver. We get notified that the data is ready, and then we 
    * put the content we receive (a string) into the TextView. 
    */ 
    public BroadcastReceiver receiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      String location; 
      // clear the progress indicator 
      if (progress != null) { 
       progress.dismiss(); 
      } 
      String response = intent.getStringExtra(RestTask.HTTP_RESPONSE); 
      try { 
       JSONObject mainObject = new JSONObject(response); 
       JSONArray tank_data = mainObject.getJSONArray("tank_data"); 
       JSONObject tankObj = tank_data.getJSONObject(0); 
       location = (String) tankObj.getString("Location"); 

      } catch (JSONException e) { 
       location = null; 
       e.printStackTrace(); 
      } 

      new TankView(context, location); 
      Log.i(TAG, "RESPONSE = " + location); 

     } 
    }; 

} 

public class RestTask extends AsyncTask<HttpUriRequest, Void, String> 
{ 
    private static final String TAG = "AsyncRestTask"; 
    public static final String HTTP_RESPONSE = "httpResponse"; 

    private Context mContext; 
    private HttpClient mClient; 
    private String mAction; 

    public RestTask(Context context, String action) { 
     mContext = context; 
     mAction = action; 
     mClient = new DefaultHttpClient(); 
    } 

    public RestTask(Context context, String action, HttpClient client) { 
     mContext = context; 
     mAction = action; 
     mClient = client; 
    } 

    @Override 
    protected String doInBackground(HttpUriRequest... params) { 
     try { 
      HttpUriRequest request = params[0]; 
      HttpResponse serverResponse = mClient.execute(request); 
      BasicResponseHandler handler = new BasicResponseHandler(); 
      return handler.handleResponse(serverResponse); 
     } 
     catch (Exception e) { 
      // TODO handle this properly 
      e.printStackTrace(); 
      return ""; 
     } 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     Log.i(TAG, "RESULT = " + result); 
     Intent intent = new Intent(mAction); 
     intent.putExtra(HTTP_RESPONSE, result); 

     // broadcast the completion 
     mContext.sendBroadcast(intent); 
    } 

} 

package com.alvinalexander.asynctest; 

import android.app.Activity; 
import android.content.res.Configuration; 
import android.os.Bundle; 

public class TestActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     if (savedInstanceState == null) { 
      TestFragment testFragment = new TestFragment(); 
      getFragmentManager().beginTransaction().add(android.R.id.content, testFragment).commit(); 
      setContentView(new TankView(this, "")); 
     } 
    } 

} 

public class TankView extends View { 

    private String location; 
    private Paint _paintTank = new Paint(); 

    public TankView(Context context, String location) { 
     super(context); 
     // TODO Auto-generated constructor stub 
     init(null, 0); 
     this.location = location; 
    } 

    public TankView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     // TODO Auto-generated constructor stub 
     init(attrs, 0); 
    } 

    public TankView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     // TODO Auto-generated constructor stub 
     init(attrs, 0); 
    } 

    public TankView(TestActivity testActivity, String location) { 
     super(testActivity); 
     this.location = location; 

    } 

    public TankView(TestActivity testActivity, Object o) { 
     super(testActivity, (AttributeSet) o); 
    } 

    private void init(AttributeSet attrs, int defStyle) { 
     _paintTank.setColor(Color.RED); 
     _paintTank.setAntiAlias(true); 
     _paintTank.setStyle(Paint.Style.STROKE); 
    } 



    @Override 
    public void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     if (this.location != null) { 
      canvas.drawText(this.location, 10, 10, _paintTank); 
     } 
    } 
} 

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:background="#AEECFF"> 

    <!-- fill the screen with a textview. the app will write text into it. --> 

    <com.alvinalexander.asynctest.TankView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</RelativeLayout> 
相關問題