添加简单http访问功能
This commit is contained in:
		| @@ -46,6 +46,8 @@ android { | ||||
| dependencies { | ||||
|     api fileTree(dir: 'libs', include: ['*.jar']) | ||||
|      | ||||
|     api 'com.squareup.okhttp3:okhttp:4.4.1' | ||||
|      | ||||
|     // 吐司类库 | ||||
|     api 'com.github.getActivity:ToastUtils:10.5' | ||||
|  | ||||
|   | ||||
| @@ -1,8 +1,8 @@ | ||||
| #Created by .winboll/winboll_app_build.gradle | ||||
| #Thu Mar 27 11:38:18 GMT 2025 | ||||
| #Thu Mar 27 12:33:37 GMT 2025 | ||||
| stageCount=0 | ||||
| libraryProject= | ||||
| baseVersion=15.0 | ||||
| publishVersion=15.0.0 | ||||
| buildCount=12 | ||||
| buildCount=17 | ||||
| baseBetaVersion=15.0.1 | ||||
|   | ||||
| @@ -4,6 +4,9 @@ | ||||
|     xmlns:tools="http://schemas.android.com/tools" | ||||
|     package="cc.winboll.studio.ollama"> | ||||
|  | ||||
|     <!-- 拥有完全的网络访问权限 --> | ||||
|     <uses-permission android:name="android.permission.INTERNET"/> | ||||
|  | ||||
|     <application | ||||
|         tools:replace="android:appComponentFactory" | ||||
|         android:appComponentFactory="android.support.v4.app.CoreComponentFactory" | ||||
| @@ -36,4 +39,4 @@ | ||||
|  | ||||
|     </application> | ||||
|  | ||||
| </manifest> | ||||
| </manifest> | ||||
| @@ -2,11 +2,18 @@ package cc.winboll.studio.ollama; | ||||
|  | ||||
| import android.app.Activity; | ||||
| import android.os.Bundle; | ||||
| import android.view.View; | ||||
| import android.widget.Button; | ||||
| import android.widget.EditText; | ||||
| import android.widget.TextView; | ||||
| import cc.winboll.studio.libappbase.LogView; | ||||
|  | ||||
| public class MainActivity extends Activity { | ||||
|  | ||||
|     LogView mLogView; | ||||
|     TextView mtvMeaasge; | ||||
|     EditText metAsk; | ||||
|     Button mbtSend; | ||||
|  | ||||
|     @Override | ||||
|     protected void onCreate(Bundle savedInstanceState) { | ||||
| @@ -15,6 +22,10 @@ public class MainActivity extends Activity { | ||||
|  | ||||
|         mLogView = findViewById(R.id.logview); | ||||
|         mLogView.start(); | ||||
|          | ||||
|         mtvMeaasge = findViewById(R.id.message_tv); | ||||
|         metAsk = findViewById(R.id.ask_et); | ||||
|         mbtSend = findViewById(R.id.send_bt); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
| @@ -22,4 +33,10 @@ public class MainActivity extends Activity { | ||||
|         super.onResume(); | ||||
|         mLogView.start(); | ||||
|     } | ||||
|      | ||||
|     public void onSend(View view) { | ||||
|         OllamaClient.SyncAskThread thread = new OllamaClient.SyncAskThread(mtvMeaasge.getText().toString()); | ||||
|         thread.start(); | ||||
|         mtvMeaasge.setText(""); | ||||
|     } | ||||
| } | ||||
|   | ||||
							
								
								
									
										108
									
								
								ollama/src/main/java/cc/winboll/studio/ollama/OllamaClient.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										108
									
								
								ollama/src/main/java/cc/winboll/studio/ollama/OllamaClient.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,108 @@ | ||||
| package cc.winboll.studio.ollama; | ||||
|  | ||||
| /** | ||||
|  * @Author ZhanGSKen@AliYun.Com | ||||
|  * @Date 2025/03/27 19:55:28 | ||||
|  * @Describe 简单Http协议访问客户端 | ||||
|  */ | ||||
| import java.io.IOException; | ||||
| import okhttp3.MediaType; | ||||
| import okhttp3.OkHttpClient; | ||||
| import okhttp3.Request; | ||||
| import okhttp3.RequestBody; | ||||
| import okhttp3.Response; | ||||
| import org.json.JSONArray; | ||||
| import org.json.JSONObject; | ||||
| import org.json.JSONException; | ||||
| import cc.winboll.studio.libappbase.LogUtils; | ||||
|  | ||||
| public class OllamaClient { | ||||
|  | ||||
|     public static final String TAG = "OllamaClient"; | ||||
|  | ||||
|     //private static final String API_BASE_URL = "http://localhost:11434"; | ||||
|     private static final String API_BASE_URL = "https://ollama.winboll.cc"; | ||||
|     private static final OkHttpClient client = new OkHttpClient(); | ||||
|  | ||||
|     // 1. 生成文本示例 | ||||
|     static void generateText(String prompt, String model) { | ||||
|         String url = API_BASE_URL + "/api/generate"; | ||||
|         try { | ||||
|             JSONObject payload = new JSONObject() | ||||
|                 .put("model", model) | ||||
|                 .put("prompt", prompt) | ||||
|                 .put("temperature", 0.7) | ||||
|                 .put("max_tokens", 200); | ||||
|  | ||||
|             Request request = new Request.Builder() | ||||
|                 .url(url) | ||||
|                 .post(RequestBody.create(payload.toString(), MediaType.get("application/json"))) | ||||
|                 .build(); | ||||
|  | ||||
|             Response response = client.newCall(request).execute(); | ||||
|             if (response.isSuccessful()) { | ||||
|                 String result = response.body().string(); | ||||
|                 JSONObject json = new JSONObject(result); | ||||
|                 LogUtils.d(TAG, "生成结果: " + json.getString("response")); | ||||
|                 //System.out.println("生成结果: " + json.getString("response")); | ||||
|             } else { | ||||
|                 LogUtils.d(TAG, "请求失败: " + response.code()); | ||||
|                 //System.out.println("请求失败: " + response.code() + " " + response.message()); | ||||
|             } | ||||
|         } catch (JSONException|IOException e) { | ||||
|             LogUtils.d(TAG, e, Thread.currentThread().getStackTrace()); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     // 2. 获取模型列表示例 | ||||
|     static void getModelList() { | ||||
|         String url = API_BASE_URL + "/api/models"; | ||||
|         LogUtils.d(TAG, "url : " + url); | ||||
|         Request request = new Request.Builder().url(url).build(); | ||||
|  | ||||
|         try { | ||||
|             Response response = client.newCall(request).execute(); | ||||
|             if (response.isSuccessful()) { | ||||
|                 JSONArray models = new JSONArray(response.body().string()); | ||||
|                 //System.out.println("可用模型列表:"); | ||||
|                 LogUtils.d(TAG, "可用模型列表:"); | ||||
|                 for (int i = 0; i < models.length(); i++) { | ||||
|                     JSONObject model = models.getJSONObject(i); | ||||
|                     LogUtils.d(TAG, "- " + model.getString("name") + " (" + model.getString("size") + ")"); | ||||
|                     //System.out.println("- " + model.getString("name") + " (" + model.getString("size") + ")"); | ||||
|                 } | ||||
|             } else { | ||||
|                 LogUtils.d(TAG, "获取模型列表失败: " + response.code()); | ||||
|                 //System.out.println("获取模型列表失败: " + response.code()); | ||||
|             } | ||||
|         } catch (JSONException | IOException e) { | ||||
|             LogUtils.d(TAG, e, Thread.currentThread().getStackTrace()); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     static void unittest(String ask) { | ||||
|         // 获取模型列表 | ||||
|         getModelList(); | ||||
|  | ||||
|         // 生成文本 | ||||
|         generateText(ask, "llama2"); | ||||
|     } | ||||
|      | ||||
|     public static class SyncAskThread extends Thread { | ||||
|         String ask; | ||||
|         public SyncAskThread(String ask) { | ||||
|             this.ask = ask; | ||||
|         } | ||||
|  | ||||
|         @Override | ||||
|         public void run() { | ||||
|             super.run(); | ||||
|             LogUtils.d(TAG, "run() start."); | ||||
|             unittest(this.ask); | ||||
|             LogUtils.d(TAG, "run() end."); | ||||
|         } | ||||
|          | ||||
|     } | ||||
| } | ||||
|      | ||||
|  | ||||
| @@ -10,13 +10,45 @@ | ||||
| 		android:layout_width="match_parent" | ||||
| 		android:layout_height="0dp" | ||||
| 		android:gravity="center_vertical|center_horizontal" | ||||
| 		android:layout_weight="1.0"> | ||||
| 		android:layout_weight="1.0" | ||||
| 		android:orientation="vertical"> | ||||
|  | ||||
| 		<TextView | ||||
| 			android:layout_width="wrap_content" | ||||
| 		<ScrollView | ||||
| 			android:layout_width="match_parent" | ||||
| 			android:layout_height="0dp" | ||||
| 			android:layout_weight="1.0"> | ||||
|  | ||||
| 			<TextView | ||||
| 				android:layout_width="match_parent" | ||||
| 				android:layout_height="match_parent" | ||||
| 				android:text="Ollama" | ||||
| 				android:textAppearance="?android:attr/textAppearanceLarge" | ||||
| 				android:id="@+id/message_tv"/> | ||||
|  | ||||
| 		</ScrollView> | ||||
|  | ||||
| 		<LinearLayout | ||||
| 			android:orientation="horizontal" | ||||
| 			android:layout_width="match_parent" | ||||
| 			android:layout_height="wrap_content" | ||||
| 			android:text="Ollama" | ||||
| 			android:textAppearance="?android:attr/textAppearanceLarge"/> | ||||
| 			android:gravity="center_vertical"> | ||||
|  | ||||
| 			<EditText | ||||
| 				android:layout_width="0dp" | ||||
| 				android:ems="10" | ||||
| 				android:layout_height="wrap_content" | ||||
| 				android:id="@+id/ask_et" | ||||
| 				android:layout_weight="1.0" | ||||
| 				android:text="Hello, World!"/> | ||||
|  | ||||
| 			<Button | ||||
| 				android:layout_width="wrap_content" | ||||
| 				android:layout_height="wrap_content" | ||||
| 				android:text="Send" | ||||
| 				android:id="@+id/send_bt" | ||||
| 				android:onClick="onSend"/> | ||||
|  | ||||
| 		</LinearLayout> | ||||
|  | ||||
| 	</LinearLayout> | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 ZhanGSKen
					ZhanGSKen