2011-06-15 60 views
0

我已经编写了一个将调用REST Web服务的Android应用程序。 首先,我试着用变量固定的Android应用程序。我可以成功地调用Web服务。为什么我在android中遇到异常

但是,当我在Android应用程序中集成了一个图形用户界面和可变分配的值,在运行时我得到以下异常:

java.lang.reflect.InvocationTargetException 
java.lang.NullPointerException 

为什么我得到的异常?

我的程序代码是:

public class testing extends Activity { 

private EditText txturl,foldername,metadata,Username,Password; 

private TextView textview; 

private Button btnok,btncancel; 

private String url,foldname,Metadata; 

private String username,password; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main);   


    btnok = (Button)findViewById(R.id.btncrtfold); 

    btncancel=(Button)findViewById(R.id.btnreset); 


      btnok.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View arg0) { 
       txturl=(EditText)findViewById(R.id.txturl); 

      Username=(EditText)findViewById(R.id.txtloginname); 

      Password=(EditText)findViewById(R.id.txtpassword); 

      foldername = (EditText)findViewById(R.id.txtfoldername); 

      metadata=(EditText)findViewById(R.id.txtmetadata); 

     foldname=foldername.getText().toString(); 

     Metadata=metadata.getText().toString(); 

     username=Username.getText().toString(); 

     url=txturl.getText().toString(); 

     password=Password.getText().toString(); 


      //HttpRequestInterceptor preemptiveAuth=login(); 
      String result=doprocess(url,username,password,Metadata,foldname); 
      textview.setText(result); 
    }}); 
} 

     public String doprocess(String url,String username,String password,String metadata,String fold){ 
      HttpResponse response=null; 
     String content=null; 

     //public static void login() { 
       HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() { 
         @Override 
         public void process(HttpRequest request, 
           HttpContext context) throws HttpException, 
           IOException { 
          AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); 
           CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(
             ClientContext.CREDS_PROVIDER); 
           HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); 

           if (authState.getAuthScheme() == null) { 
            AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); 
            Credentials creds = credsProvider.getCredentials(authScope); 
            if (creds != null) { 
             authState.setAuthScheme(new BasicScheme()); 
             authState.setCredentials(creds); 
            } 
          } 
         }  
        }; 



     DefaultHttpClient httpclient = new DefaultHttpClient(); 
     url= url+"/"+username+"/"+foldername; 
     HttpPut put=new HttpPut(url); 
     try{ 
     httpclient.getCredentialsProvider().setCredentials(
       new AuthScope(null, -1), 
       new UsernamePasswordCredentials(username, password)); 
     httpclient.addRequestInterceptor(preemptiveAuth, 0); 
     HttpEntity data=new StringEntity(metadata); 
     put.setEntity(data); 
     put.setHeader("Content-Type","application/vnd.org.snia.cdmi.container"); 
     response = httpclient.execute(put); 
     HttpEntity resEntity=response.getEntity(); 
     if(resEntity!=null) 
     content = (EntityUtils.toString(resEntity)); 
     }catch (Exception e) { 
     Log.e("PUT Exception", "java.lang.NullPointerException"); 
     } 
     String result=response.getStatusLine().toString()+"\n"+content; 

     return result; 
} 
+3

如果您可以发布整个堆栈跟踪,那将会很棒。 – levis501 2011-06-15 06:43:25

+0

不要在你的ip – ingsaurabh 2011-06-15 06:44:39

+0

...和实际抛出异常的代码之后使用端口号:8080,我怀疑它是'doprocess'。 – 2011-06-15 06:45:37

回答

1

您好我想你doProcess方法不返回任何东西,这样它会扔的setText

NullPointerException异常上的TextView的方法。

请确认。

+0

嗨Anup,我想是的。在程序中,当我调试时,结果为空,并返回null。我不知道为什么 – sudo 2011-06-15 07:43:26

+0

由于您的HttpResponse尚未收到,您正在对该结果进行操作。因此,Http调用应该被阻止。 – 2011-06-15 08:02:46

+0

嗨Anup Rojekar,我收到回应字符串。但我在设置字符串时出错。我可以在textview中设置字符串:“textview.setText(result);” – sudo 2011-06-15 09:18:35

相关问题