Android:消费休息服务。 “SyncServices / ValidateUser?SName = {SITENAME}”+正文

时间:2013-07-15 19:28:27

标签: android json rest httprequest

这是其他服务的参考:(如果有助于识别使用的约定等,则在ASP.net中编写)。我对Web服务一般都是新手,所以请假设我知之甚少。

// X.X.X.X:X / SyncServices /的ValidateUser SNAME = {SITENAME}

以下是Json正文的一个示例请求: {     “密码”:“字符串内容”,     “UserName”:“字符串内容” }

所以它似乎要我发送SName作为参数,然后附加一个正文。 这是我的doInBackground方法。

        @Override
    protected String doInBackground(String... uri) {

        URI website = null;
        JSONObject user =  new JSONObject();
        HttpClient httpclient = new DefaultHttpClient();            
        HttpPost request = new HttpPost();

        try {
            user.put("Password", "test");
            user.put("UserName", "user");                               
            website = new URI( uri[0] );

            ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
            postParameters.add(new BasicNameValuePair("SName", "site"));

            request.setEntity(new StringEntity(user.toString()));
            request.setURI(website);
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        } catch (JSONException e1) {
            e1.printStackTrace();
        } catch (URISyntaxException e2) {
            e2.printStackTrace();
        }

        HttpResponse response;
        String responseString = null;
        try {
            response = httpclient.execute(new HttpGet(uri[0]));
            StatusLine statusLine = response.getStatusLine();
            if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                response.getEntity().writeTo(out);
                out.close();
                responseString = out.toString();
            } else{
                //Closes the connection.
                response.getEntity().getContent().close();
                throw new IOException(statusLine.getReasonPhrase());
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return responseString;
    }
07-15 11:30:53.935: D/libEGL(2131): loaded /system/lib/egl/libEGL_emulation.so
07-15 11:30:53.946: D/(2131): HostConnection::get() New Host Connection established 0xb83d5580, tid 2131
07-15 11:30:53.958: D/libEGL(2131): loaded /system/lib/egl/libGLESv1_CM_emulation.so
07-15 11:30:53.965: D/libEGL(2131): loaded /system/lib/egl/libGLESv2_emulation.so
07-15 11:30:54.265: W/System.err(2131): java.io.IOException: Method Not Allowed
07-15 11:30:54.265: W/System.err(2131):     at com.example.httptest.MainActivity$RequestTask.doInBackground(MainActivity.java:112)
07-15 11:30:54.265: W/System.err(2131):     at com.example.httptest.MainActivity$RequestTask.doInBackground(MainActivity.java:1)
07-15 11:30:54.265: W/System.err(2131):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-15 11:30:54.265: W/System.err(2131):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-15 11:30:54.265: W/System.err(2131):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-15 11:30:54.265: W/System.err(2131):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-15 11:30:54.265: W/System.err(2131):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-15 11:30:54.265: W/System.err(2131):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-15 11:30:54.265: W/System.err(2131):     at java.lang.Thread.run(Thread.java:856)
07-15 11:30:54.296: W/EGL_emulation(2131): eglSurfaceAttrib not implemented
07-15 11:30:54.325: D/OpenGLRenderer(2131): Enabling debug mode 0
07-15 11:30:54.925: W/InputMethodManager(2131): Ignoring onBind: cur seq=17, given seq=16

我的代码可以使用(见下文) 但是,在我更加加密用户名,密码等之前。我如何(我应该?)从我的uri末尾提取这个“?SName = [sNameVarHere]”,这样我就不会做类似的事了。 fullUri = baseUri +“?SName =”+ sName;或者这是可以接受的吗?

static final String TAG = "httpTest";
RequestTask task;
EditText txtResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txtResult = (EditText) findViewById(R.id.editText1);        
    task = (RequestTask) new RequestTask();
    task.execute("http://x.x.x.x:x/RestSyncServices/ValidateUser?SName=[sNameVarHere]"); 
}
    class RequestTask extends AsyncTask<String, String, String>{
    @Override
    protected String doInBackground(String... uri) {

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(uri[0]);           

        JSONObject user =  new JSONObject();
        try {               
            user.put("UserName", "user");
            user.put("Password", "password");

            httpPost.setHeader("Content-Type", "application/json");
            httpPost.setEntity(new StringEntity(user.toString() ));

            HttpResponse resp = httpclient.execute(httpPost);
            Log.i(TAG, Integer.toString( resp.getStatusLine().getStatusCode()  ) );         

        } catch (JSONException e) {
            e.printStackTrace();                    
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

0 个答案:

没有答案