Question

I'm struggling to retreive some data from my localhost Solr from an Android Application. but i'm still having the same error.

05-06 18:22:09.036: E/AndroidRuntime(1628): java.lang.NoSuchMethodError: org.apache.http.conn.scheme.Scheme.<init>
05-06 18:22:09.036: E/AndroidRuntime(1628): at org.apache.http.impl.conn.SchemeRegistryFactory.createSystemDefault(SchemeRegistryFactory.java:83) 
05-06 18:22:09.036: E/AndroidRuntime(1628): at org.apache.http.impl.client.SystemDefaultHttpClient.createClientConnectionManager(SystemDefaultHttpClient.java:121) 
05-06 18:22:09.036: E/AndroidRuntime(1628): at org.apache.http.impl.client.AbstractHttpClient.getConnectionManager(AbstractHttpClient.java:221)

I'm using these Jars : - Commons-io-2.1.jar

  • httpclient-4.3.1.jar
  • httpcore-4.3.jar
  • jcl-over-slf4j-1.6.6.jar
  • jul-to-slf4j-1.6.6.jar
  • log4j-1.2.16.jar
  • slf4j-api-1.6.6.jar
  • slf4j-log4j12-1.6.6.jar
  • solr-solrj-4.8.0jar

and this is the code I wrote : package com.example.secondapp;

import java.net.MalformedURLException; 

import org.apache.solr.client.solrj.SolrQuery; 
import org.apache.solr.client.solrj.SolrServer; 
import org.apache.solr.client.solrj.SolrServerException; 
import org.apache.solr.client.solrj.impl.HttpSolrServer; 
import org.apache.solr.client.solrj.response.QueryResponse; 
import org.apache.solr.common.SolrDocumentList; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 

public class MainActivity extends Activity { 

        Button btn1; 
        EditText etxt1; 

        public void RunIt() throws MalformedURLException, SolrServerException 
        { 
                String urlString = "http://localhost:8983/solr/collection1"; 

    SolrServer solr = new HttpSolrServer(urlString); 

    SolrQuery parameters = new SolrQuery(); 

    parameters.set("qt", "/select"); 
                parameters.set("q", "mem"); 

                QueryResponse response = solr.query(parameters); 
                SolrDocumentList list = response.getResults(); 


                   // for (int i = 0; i < list.size(); ++i) { 

                     // etxt1.setText((CharSequence) list.get(i)); 
                    //} 

        } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 

        btn1=(Button)findViewById(R.id.button1); 
        etxt1=(EditText)findViewById(R.id.editText1); 


        btn1.setOnClickListener(new View.OnClickListener() { 

                        @Override 
                        public void onClick(View v) { 


                                etxt1.setText("a fin111"); 


                                try { 
                                        RunIt(); 
                                } catch (MalformedURLException e) { 
                                        // TODO Auto-generated catch block 
                                        e.printStackTrace(); 
                                } catch (SolrServerException e) { 
                                        // TODO Auto-generated catch block 
                                        e.printStackTrace(); 
                                } 


                        } 
                }); 
            } 
}

can anyone tell me the mistake thanks.

Further Investigations show that solrj depends on the old httpclient shipped in the android tools, and doesn't use that httpclient-4.3.1.jar I referenced.

what can do to solve this.

Was it helpful?

Solution

SOLVED,

I didn't use SOLRJ ....

I used just the simple http get request, and it worked fine (with a small delay) toguether with asynctask for background thread.

this is the code.

package com.example.secondapp;
import com.example.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.DefaultClientConnection;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


 public class MainActivity extends Activity {

    Button btn1;
     EditText etxt1;
     TextView tvHttp;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //btn1=(Button)findViewById(R.id.button1);
       // etxt1=(EditText)findViewById(R.id.editText1);
        tvHttp = (TextView) findViewById(R.id.textView1);


        HttpGetter Obj = new HttpGetter();
        Obj.execute("http://10.0.2.2:8983/solr/collection1/select?q=mem&wt=json&indent=true");


        //StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());


    }



    public  class HttpGetter  extends AsyncTask <String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub

        BufferedReader reader=null;
        String data =null;

         try {


             HttpClient client = new DefaultHttpClient();

             URI uri = new URI(params[0]);

             HttpGet Gett = new HttpGet(uri);

             HttpResponse response= client.execute(Gett);

             InputStream stream = response.getEntity().getContent();

             reader = new BufferedReader(new InputStreamReader(stream));

             StringBuffer buffer = new StringBuffer("");
             String line="";
             String newline=System.getProperty("line.separator");

             while ((line= reader.readLine())!=null)
             {
                 buffer.append(line + newline);
             }
             reader.close();

             data= buffer.toString();
             return data;

         }

         catch (URISyntaxException e)
         {
             e.printStackTrace();
         } 
         catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally
        {
            if (reader != null){
                try{
                    reader.close();
                }catch (Exception e){

                }
            }

        }



         return null;

}

   @Override
   protected void onPostExecute(String result)
   {
       super.onPostExecute(result);
       tvHttp.setText(result);
   }


}}

OTHER TIPS

I believe you need to use the SLF4J-Android jars.

http://www.slf4j.org/android/

I also had to Override the Standard SORLJ Util And Server classes like here.

https://gist.github.com/soltrinox/4b1b85509699c718b5d9

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top