0

这是其余服务的参考:(写在 ASP.net 中,如果它有助于确定使用了哪些约定等......)。一般来说,我对 Web 服务真的很陌生,所以请假设我知道的很少。

//xxxx:x/SyncServices/ValidateUser?SName={SITENAME}

以下是一个示例请求 Json 正文: { "Password":"String content", "UserName":"String content" }

所以它似乎希望我将 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;
    }
4

0 回答 0